Javascript 改变css类,直接操作css,非遍历所有dom法

时间:2021-08-24 12:22:52
<div id="test_chg">
Look At Me,<a href="javascript:gogo();void(0)">Change me</a>!<br />
<span id="curcss"></span>
</div>
<script language="javascript">
var g={
E:
function (c) {
if (typeof c === "string") {
return document.getElementById(c);
}
else {
return c;
}
},
C:
function (c) {
var d;
c
= c.toUpperCase();
if (c == "TEXT") {
d
= document.createTextNode("");
}
else {
if (c == "BUFFER") {
d
= document.createDocumentFragment();
}
else {
d
= document.createElement(c);
}
}
return d;
}
}
function gogo()
{
var y = g.E("custom_style");
if (!g.E("custom_style")) {
y
= g.C("style");
y.setAttribute(
"type", "text/css");
y.id
= "custom_style"
}
W
=[];
W.push(
'#test_chg{color:'+'rgb('+rand(1,255)+','+rand(1,255)+','+rand(1,255)+');');
W.push(
'font-size:'+rand(21,32)+'px}');
document.getElementsByTagName(
"head")[0].appendChild(y);
var N=W.join('\n');
if (y.styleSheet) {
y.styleSheet.cssText
= N
}
else {
y.innerHTML
= N
}
g.E(
'curcss').innerHTML=N
}
</script>