用Javascript调用css的类 怎样用Javascript代码调用css中的类,请高手指点 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没办法,没有原生的 document.getElementsByClass只能先用别的办法遍历 再判断,比如 odivs = document.getElementsByTagName("DIV"); if (odivs[i].className == "myclass") { } <div id="demo" class="test"></div><script type="text/javascript">alert(document.getElementById('demo').className);</script> 如:<div id="div1"></div>css:.divCssjs:document.getElementsByID("div1").className="divCss"; dom中class就是 楼主所说style中就这样<!DOCTYPE HTML><html> <head> <meta charset="gb2312" /> <title></title> </head> <body> <div class="a">111111111</div> <div class="b">111111111</div> <div class="c">111111111</div> <script> function $(el){ return typeof el == 'string' ? document.getElementById(el) : el; } ;(function(w, d){ var ie = !+[1,]; var o = null; var styleSheet = function(){ this.init(); }; styleSheet.prototype = { init: function(){ var h = d.getElementsByTagName('head')[0]; var s = d.createElement('style'); h.appendChild(s); o = d.styleSheets[d.styleSheets.length-1]; }, add: function(selector, style){ ie ? o.addRule(selector, style) : o.insertRule(selector + "{" + style + "}", o.cssRules.length); }, del: function(index){ ie ? o.removeRule(index) : o.deleteRule(index); }, getRules: function(){ return ie ? o.rules : o.cssRules; }, getRule: function(selector){ var rules = this.getRules(); for(var i = 0, len = rules.length; i < len; i++){ var r = rules[i]; if( r.selectorText == selector ){ return r; } } return null; } } w.styleSheet = styleSheet; })(window, document); var s = new styleSheet; s.add('.a', 'color:red;') s.add('.a', 'font-size:111px;') s.add('.a', 'font-size:11px;') s.add('.b', 'color:blue;') //s.del(0) alert( s.getRule('.a').style['color'] ) </script> </body></html> 这貌似说反了吧,应该是css类中定义的样式啊,CSS本身又不能定义类,类名是在HTML中定义的,LZ直接说想干什么得了 不明白楼主的意思。如果是改变已有内容的class的话,用jquery吧$("div").removeClass('').addClass(''); -------------------JS拼接和分割方法----------------------- wrapInner方法不太明白 这段JS代码什么意思 为什么不能使窗口根据location.hash有所反应 javascript怎么改变函数里的属性? 求救,js JS中,OPTIONS属性中的取值范围以及I++的用法问题。 急问题!怎么在javascript里读取库啊!! 怎样从动态生成的表单组件<type=text>中读出value的值 红警2错误提示!是什么原因??急在线等待!!!! JSON问题 酷狗主页上那个实现图片切换效果的代码在哪里?
只能先用别的办法遍历 再判断,比如
odivs = document.getElementsByTagName("DIV");
if (odivs[i].className == "myclass") { }
alert(document.getElementById('demo').className);
</script>
css:
.divCss
js:
document.getElementsByID("div1").className="divCss";
style中就这样<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
</head>
<body>
<div class="a">111111111</div>
<div class="b">111111111</div>
<div class="c">111111111</div>
<script>
function $(el){
return typeof el == 'string' ? document.getElementById(el) : el;
}
;(function(w, d){
var ie = !+[1,];
var o = null;
var styleSheet = function(){
this.init();
};
styleSheet.prototype = {
init: function(){
var h = d.getElementsByTagName('head')[0];
var s = d.createElement('style');
h.appendChild(s);
o = d.styleSheets[d.styleSheets.length-1];
},
add: function(selector, style){
ie ?
o.addRule(selector, style)
:
o.insertRule(selector + "{" + style + "}", o.cssRules.length);
},
del: function(index){
ie ?
o.removeRule(index)
:
o.deleteRule(index);
},
getRules: function(){
return ie ?
o.rules
:
o.cssRules;
},
getRule: function(selector){
var rules = this.getRules();
for(var i = 0, len = rules.length; i < len; i++){
var r = rules[i];
if( r.selectorText == selector ){
return r;
}
}
return null;
}
}
w.styleSheet = styleSheet;
})(window, document);
var s = new styleSheet;
s.add('.a', 'color:red;')
s.add('.a', 'font-size:111px;')
s.add('.a', 'font-size:11px;')
s.add('.b', 'color:blue;')
//s.del(0)
alert( s.getRule('.a').style['color'] )
</script>
</body>
</html>