61,112
社区成员
发帖
与我相关
我的任务
分享
element.currentStyle.width;
window.getComputedStyle(element,null).width;
window.getComputedStyle(document.getElementByClassName("clearfix")[0],"after").content;
W3C标准使用window.getComputedStyle,带两个参数,第一个是element,第二个是元素的伪类,可以为null或不填。var getStyle=function(){
var f=document.defaultView;
return new Function('el','style',[
"style.indexOf('-')>-1 && (style=style.replace(/-(\\w)/g,function(m,a){return a.toUpperCase()}));",
"style=='float' && (style='",f ? 'cssFloat' : 'styleFloat',"');return el.style[style] || ",
f ? 'window.getComputedStyle(el, null)[style]' : 'el.currentStyle[style]',' || null;'
].join(''));
}();
这段代码是以前上网找的,现在不知道链接地址了。<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.onload = function(){
var style;
var elem = document.getElementById("mybutton");
if(document.defaultView && document.defaultView.getComputedStyle){
style = window.getComputedStyle(elem).getPropertyValue("visibility");
}else if(elem.currentStyle){
style = elem.currentStyle[ "visibility" ];
}
alert(style);
};
</script>
</head>
<body>
<div style="visibility:hidden">
<input id="mybutton" type="button" style="visiblity:inherit"></input>
</div>
</body>
</html>
测了IE7,8和Firefox, IE8和Firefox返回"hidden", IE7返回"inherit", 没找到IE7下的解决方案。<!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>input</title>
<script type="text/javascript">
window.onload=function(){
var oBtn = document.getElementById('mybutton');
if(oBtn.style.visibility=='hidden'){ //如果是隐藏,则显示
//alert(0);
oBtn.style.visibility='visible';
}else{
oBtn.style.visibility='hidden'; //隐藏
}
}
</script>
</head>
<body style="height:5000px;">
<!--不懂你父级,也加 visibility:inherit;干嘛的-->
<div style=" visibility:inherit;">
<input id="mybutton" type="button" style="visibility:hidden;" value="测试文字" />
</div>
</body>
</html>
<!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>
<title></title>
<script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("button").click(function () {
alert($("#mybutton").css("visibility"));
alert(mybutton.style.visibility);
});
});
</script>
</head>
<body>
<div style="visibility: hidden">
<input id="mybutton" type="button" style="visibility: inherit;" />
</div>
<button>
获取visibility</button>
</body>
</html>