惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
Latest news
Latest news
Help Net Security
Help Net Security
S
Security Affairs
Webroot Blog
Webroot Blog
The Hacker News
The Hacker News
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Help Net Security
L
Lohrmann on Cybersecurity
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
MongoDB | Blog
MongoDB | Blog
Cyberwarzone
Cyberwarzone
The Last Watchdog
The Last Watchdog
S
Securelist
N
News and Events Feed by Topic
S
Secure Thoughts
F
Fortinet All Blogs
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
M
MIT News - Artificial intelligence
F
Full Disclosure
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
P
Privacy International News Feed
L
LangChain Blog
Know Your Adversary
Know Your Adversary
C
CERT Recently Published Vulnerability Notes

博客园 - 早班火车

很还念这里。。。说些什么呢。。。 程序,好亲切~ 关于javascript中的事件学习及总结 总结了几个常用的sql server系统表的使用 在做一个小网站的一些心得与遇到的问题总结,为以后方便查阅。 用sql脚本一条条导数据的两种方法,需返回唯一标识@@IDENTITY作为插入到第二个表用。 一步一步教你抓数据——用.net精确提取网站数据的通用方法 也来为自己的博客加个花,兼AJAX跨域的一点疑问。 关于IE缓存和AJAX的一点思考和疑问 退一步海阔天空:抛开思维定势 DOCTYPE:你可能不知道的 我的2007--从2008做起,加油! javascript常用验证 正则表达式参考手册__Mini版 树的操作(绑定数据库,添加新节点,删除节点)(转载加实现) 一段好用的ajax和div漂浮显示效果实现 ajax小贴士 好用的缓存类 javascript操纵剪贴板
IE与DOM下访问内联样式和外部样式表的常用方法总结
早班火车 · 2008-04-14 · via 博客园 - 早班火车

1IE与DOM下访问内联样式的常用方法

1ie下访问内联样式的方法和style的两个常用属性
1this.style.cssText
<div id="div1"  style="background-color: red; height: 50px; width: 50px"
onclick="alert(this.style.cssText)"></div>
//outputs background-color: red; height: 50px; width: 50px
2每个元素都有一个currentStyle用以反映当前style属性。
oDiv.currentStyle.backgroundColor

至于ie访问内敛样式只需写Element.style.propertyValue

2DOM下访问内联样式的常用方法.(firefox)通过Element.style访问如下方法。
1getPropertyValue(name) 返回样式值
2getPropertyPriority() 若指定important,则返回important,否则返回空。
3item(index) 返回给定的目标索引处的css属性名称
4removePropertyPriority(name) 移除指定样式。
5setProperty(propertyName,value,priority) 设置属性,最后一个为空/important

<html>
    
<head>
        
<title>Style Example</title>
        
<script type="text/javascript">
            
function useMethods() {
                
var oDiv = document.getElementById("div1");
                alert(oDiv.style.item(
0));    //outputs "background-color"
                alert(oDiv.style.getPropertyValue("background-color")); 
                oDiv.style.removeProperty(
"background-color");
    
            }
        
</script>
    
</head>
    
<body>
        
<div id="div1" style="background-color: red; height: 50px; width: 50px"  onmouseover="this.style.setProperty('background-color', 'blue', '')"
             onmouseout
="this.style.setProperty('background-color', 'red', '')"></div><br />
        
<input type="button" value="Use Methods" onclick="useMethods()" />
    
</body>
</html>

(提示:仅firefox下可以运行,IE不支持这些方法)

3IE与DOM访问外部样式的方法及常用属性
看个例子吧,里面针对IE和DOM的不同,及各个属性的使用我已经在注释里写明了。

<html>
    
<head>
        
<style type="text/css">
            div.special 
{
                background-color
: red;
                height
: 10px;
                width
: 10px;
                margin
: 10px;
            
}

        
</style>
        
<script type="text/javascript">
            
function changeBackgroundColor() {
                
var oDiv = document.getElementById("div1");
                oDiv.style.backgroundColor 
= "blue";
        
var oRules = document.styleSheets[0].cssRules || document.styleSheets[0].rules;//检测浏览器是哪个牌子的

        alert(oRules[
0].selectorText);//outputs DIV.special "selectorText"返回样式名称

        
//alert(oRules[0].cssText);//outputs undefine 属于内联样式属性

                alert(oRules[
0].style.backgroundColor);//outputs red
        alert(oDiv.style.backgroundColor); //blue 可以以数组形式访问alert(oDiv.style["backgroundColor"]); 
        //因为style本身就是一个集合。
                
        alert(oDiv.currentStyle.backgroundColor);
//for IE  //outputs blue
        //(currentStyle为所有样式作用计算得出的最终样式)实际是不存在的,所以也不能改变其值
        //只能通过属性赋值改变。
        //alert(document.defaultView.getComputedStyle(oDiv,null).backgroundColor);//for dom 类比currentStyle
        }

        
</script>
    
</head>
    
<body>
        
<div id="div1" class="special"></div>
        
<input type="button" value="Change Background Color" onclick="changeBackgroundColor()" />
    
</body>
</html>

这里要注意的是在javascript里改变的样式,实际并没有改变样式表里的属性,相当于重写了外部属性
所以用oRules[0].style.backgroundColor访问样式表的时候,其值并没有改变改变的只是当前的外部样式的属性。oDiv.style.backgroundColor

4最后举一个用javascript动态给元素批量增加style的小例子。


提示:可以先在文本框内,根据需要修改代码后再运行