왜 this.style[property]
빈 문자열을 받으시겠습니까? 내 코드는 다음과 같습니다javascript this.style [property]가 빈 문자열을 반환하는 이유는 무엇입니까?
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<style type="text/css">
#test{
height:100px;
}
.tclass{
width:100px;
}
</style>
<script type="text/javascript">
function $(ID){
var element=document.getElementById(ID||'nodId');
if(element){
element.css=css;
}
return element;
}
function css(prop,value){
if(value==null){
return this.style[prop];
}
if(prop){
this.style[prop]=value;
}
return true;
}
window.onload=function(){
var element=$("test");
alert(element.css("height")+","+element.css("width")+","+element.css("background"));
//alert ,,#ccc
};
</script>
</head>
<body>
<div id="test" style="background:#CCC;" class="tclass">Test</div>
</body>
</html>
이 코드 경고 ,,#ccc
,하지만 난 100px,100px,#ccc
를 얻을 싶어, 저와 무슨 일이야? 누가 날 도울 수 있죠?
function css(prop,value){
if(value==null){
var b = (window.navigator.userAgent).toLowerCase();
var s;
if(/msie|opera/.test(b)){
s = this.currentStyle
}else if(/gecko/.test(b)){
s = document.defaultView.getComputedStyle(this,null);
}
if(s[prop]!=undefined){
return s[prop];
}
return this.style[prop];
}
if(prop){
this.style[prop]=value;
}
return true;
}
DOM 요소에는'style' 속성이 없습니다. 그래서 당신은 그들의'style' 속성을 얻을 수 없습니다. –
'elem.style'은 요소에있는 스타일 * 속성 *에 액세스하고 있습니다. – pimvdb