logo 西林@生存 - 骑猪闯天下!
修改<STYLE>元素内的CSS!
2007-9-11 15:33:00 By:xling

大部分应用都是直接修改某个HTML元素的style或className属性来完成修改外观的。
假如现在要求把N多个具有相同className的HTML元素的外观统一调整,相信很多人都会用循环。

如果屈指可数的话,当然可以。但是如果N是一个很大的数呢?用循环略显示的有些弱智。
我的JGrid用的也是上面的笨方法(汗...)

下面我要演示的方法不常见,但也不是说以前就没有这样做的,只不过我们很少用而以(基本上没有人会去考虑):
直接修改<style>里定义的CSS。

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
<!--
* {
 font-family: Verdana, Arial, Helvetica, sans-serif;
 font-size: 12px;
}
.height {
 height:200px;
}
.width {
 width:200px;
}
.color{
 background-color:#CCCCCC;
}
-->
</style>
</head>

<body>
<hr />
Color:<br />
<input type="radio" name="radio" id="radio" value="#ff0000" onclick="c(this)" />#ff0000
<input type="radio" name="radio" id="radio2" value="#ff00ff" onclick="c(this)" />#ff00ff
<input type="radio" name="radio" id="radio3" value="#cc6600" onclick="c(this)" />#cc6600
<input type="radio" name="radio" id="radio4" value="#000000" onclick="c(this)" />#000000
<hr />
Width:
<input type="text" name="textfield" id="textfield" oninput="w(this)" onpropertychange="w(this)" />
Height:
<input type="text" name="textfield2" id="textfield2" oninput="h(this)" onpropertychange="h(this)" />
<table border="1">
  <tr>
    <td class="height width color"></td>
    <td class="height width color"></td>
    <td class="height width color"></td>
    <td class="height width color"></td>
    <td class="height width color"></td>
  </tr>
</table>
</body>
</html>
<script language="javascript" type="text/javascript">
var o;
var obj = document.styleSheets[0];
var rules = obj.rules || obj.cssRules;

var styleObj = {};
var selectorText,readonly,style,cssText;
for(var i=0;o=rules[i];i++){
 selectorText = o.selectorText;
 //readonly = new String(o.readOnly); IE
 //cssText = o.cssText; FF
 style = o.style;
 document.write(selectorText.fontcolor("#ff0000"),"<br />");
 styleObj[selectorText] = style;
}

function c(obj){
 if(obj.checked)
  styleObj[".color"].backgroundColor = obj.value;
}

function h(obj){
 try{
 styleObj[".height"].height = parseInt(obj.value) + "px";
 }catch(e){}
}

function w(obj){
 try{
 styleObj[".width"].width = parseInt(obj.value) + "px";
 }catch(e){}
}
</script>

转我的贴,写你的名,是一种可耻的行为!请不要让我BS你!
阅读全文 | 回复(0) | 引用通告 | 编辑

发表评论:

    密码:
    主页:
    标题:
    页面数据正在载入...
<<  < 2008 - >  >>
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Placard
请职:PHP或JS都行,如果学历要求本科的,请直接忽略。谢谢。 有意者请联系:1fairy1#163.com
Logon System
Search
Info about this blog
Others
myDream
JGrid

数据表格
JTree

树状控件
JMenuTab

标签控件(滑动门)
JTrackBar / JScroll

滑动条/模拟滚动条
JAccordion

(QQ菜单)
关于:
xling,也叫xlingFairy 艾克司令 西林.
Email:1fairy1#163.com
喜欢javascript,喜欢java(汗...还在学习中...水平菜的要死...)
bxna 京ICP备05002321号