一般给元素设置行内样式,如
在标准浏览器中可以通过window.getComputedStyll(obj,null)[property]来获取外链样式,但是在ie浏览器中则是通过obj.currentStyle来获取。
完整html代码:
js获取元素的外链样式p {
width: 500px;
line-height: 30px;
}
function getstyle(obj,property){
if(obj.currentStyle){
return obj.currentStyle[property];
}else if(window.getComputedStyle){
return document.defaultView.getComputedStyle(obj,null)[property];//或者也可以通过window.getComputedStyle来获取样式
}
return null;
}
$(document).ready(function(){
$("p").click(function(){
alert(getstyle(this,"width"));
});
});
如果您点击我,弹出宽度。
点击我,弹出宽度。
也要点击我哦。
以上这篇js获取元素的外链样式的简单实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。