<style type="text/css">
.a{ background-color:#00cc00; height:100px; width:100px}
.b{background-color:#cccccc}
</style><body>
<div id='test' class='a'></div>
</body>我希望的效果是
当时鼠标移动到div上的时候
div的 className变成 "a b"
鼠标离开div的时候
div的className变成"a"怎么写?
.a{ background-color:#00cc00; height:100px; width:100px}
.b{background-color:#cccccc}
</style><body>
<div id='test' class='a'></div>
</body>我希望的效果是
当时鼠标移动到div上的时候
div的 className变成 "a b"
鼠标离开div的时候
div的className变成"a"怎么写?
window.onload=function(){
var div=document.getElementById("test");
div.onmouseover=function(){this.className="a";};
div.onmouseout=function(){this.className="b";};
}
</script>
window.onload=function(){
var div=document.getElementById("test");
div.onmouseover=function(){this.className="a";};
div.onmouseout=function(){this.className="b";};
}
</script>
<style type="text/css">
.a{ background-color:#00cc00; height:100px; width:100px}
.b{ background-color:#cccccc; height:100px; width:100px}
</style> <body>
<div id='test' class='a'> </div>
</body>
鼠标放上去的时候是样式变成 "a b" 就象jq里面的addclass一样
鼠标移开的时候 变成"a" 象removeClass一样
window.onload=function(){
var div=document.getElementById("test");
div.onmouseover=function(){this.className="a b";};
div.onmouseout=function(){this.className="b";};
}注意样式b要写在a的后面
这就是直接附直
a,b是未知的 不能这么写
addClass: function(value) {
if (jQuery.isFunction(value)) {
return this.each(function(i) {
var self = jQuery(this);
self.addClass(value.call(this, i, self.attr("class")));
});
} if (value && typeof value === "string") {
var classNames = (value || "").split(rspace); for (var i = 0, l = this.length; i < l; i++) {
var elem = this[i]; if (elem.nodeType === 1) {
if (!elem.className) {
elem.className = value; } else {
var className = " " + elem.className + " ";
for (var c = 0, cl = classNames.length; c < cl; c++) {
if (className.indexOf(" " + classNames[c] + " ") < 0) {
elem.className += " " + classNames[c];
}
}
}
}
}
} return this;
}