<script type="text/javascript">
(function (bool) {
//严重感谢M2前辈
if (bool) {
HTMLElement.prototype.__defineGetter__("currentStyle", function () {
//getComputedStyle方法参数一是目标对象,参数二是具体属性。。。把注释对换下就知道了。。
//return this.ownerDocument.defaultView.getComputedStyle(this, ":first-line");
return this.ownerDocument.defaultView.getComputedStyle(this, null);
});
}
})(/Firefox/.test(window.navigator.userAgent));
</script>
<style type="text/css">
div {
color:#000000;
}
div:first-line {
color:#0000FF;
}
</style>
<div onclick="alert(this.currentStyle.color)">DIV对象<br />第二行</div>
(function (bool) {
//严重感谢M2前辈
if (bool) {
HTMLElement.prototype.__defineGetter__("currentStyle", function () {
//getComputedStyle方法参数一是目标对象,参数二是具体属性。。。把注释对换下就知道了。。
//return this.ownerDocument.defaultView.getComputedStyle(this, ":first-line");
return this.ownerDocument.defaultView.getComputedStyle(this, null);
});
}
})(/Firefox/.test(window.navigator.userAgent));
</script>
<style type="text/css">
div {
color:#000000;
}
div:first-line {
color:#0000FF;
}
</style>
<div onclick="alert(this.currentStyle.color)">DIV对象<br />第二行</div>
本文介绍了一种使用JavaScript来获取DOM元素样式的技巧,并针对不同浏览器进行了兼容性处理。通过自定义HTMLElement原型上的currentStyle属性,使得在IE和其他浏览器上都能正确获取到元素的计算样式。
249

被折叠的 条评论
为什么被折叠?



