猿问

.style样式不能获得非内联样式吗?为什我先通过js改变样式,就可以获取样式了。详见js代码

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Title</title>

    <style type="text/css">

        *{

            margin: 0;

            padding: 0;

        }

        div {

            width: 1000px;

            height: 300px;

            text-align: center;

            background: #0e90d2;

            color: #d3eff9;

            font-family: '微软雅黑';

            line-height: 300px;

        }

        p {

            font-size: 20px;

        }

 .answer{

display:none;

}

    </style>

</head>

<body>


<div>

    <p>鼠标经过文字将会缩放</p>

</div>

<script type="text/javascript">

window.onload=function(){

  div=document.getElementsByTagName("div")[0];

  div.onmouseover=function(){

     startMove();

  }

}

function startMove(){

//情况一:尚未改变div的样式,能够获取非内联样式

alert(div.style.width);

/*

情况二:改变了div的样式,能够获取非内联样式

div.style.width=300+"px";

alert(div.style.width);

*/

}

</script>

</body>


</html>


每天固定就好
浏览 2629回答 3
3回答

慕的地8582982

GMEPYVLYHBCLIIJHREWPJQGILOXVKHWYBSPLOEUKAXGDNVYIYILOKNDAWGCGPSIEGXILBYCDMPLUKRGWFHSUYAXHNUQAWNROLORLVXHKBKNPMVWSULBRUKZCFHCNJNWMPKBEIKGVLIETBYVUCFGPMCMVFHVKNIXARHJMCSBKGRTOYAJGYODMCYWFBYBOEAWADTBRUXISBBHBIYMTQFBERRHUZYVJIPILIJZPECFXJGMQKXJKXPCYDRBUOXDDDBXRYOOUENEXZFVZPAGTJAKLYDWZOXQJDGSEYVPJJAWPADMSIITJGSXGGGCMPQJRGDXWKZFLFEIYOIBBFVLVZXQHKKJVFFOLIXXEVYOYYIGLERXLSEMPPVVUUFCWCSSDBUTNXVELVOPTZOISWWITTXDGWZCZBNXZIQPFIDBWJJUEFJOICRKUNXRISIPICIPCCWZJLRXSUULPPXLUYIMJLBKVFPFMCZFLYLZWPQKHYWLFPQXPPMWDZVLVQTQGQRVMOYNXAQLIFETKTWMVLOCFHDMPTDZPDGCHKZWTBLOEAKAWMCSBEAURTLGEHKHYOROEGPSJZPLVLBXWSNLUKNQRHXJQNRHKADADTEOYHXHQGVEGCOURICTNNBTZIIREEPZLCPRRDNVVHKNXRIOAEHFIYHRLLLBSSRSNZJRCZFZSCTQJGJETJJJNJNQXTKSYRBKRSEBRKQDEOVOWIZPAEVIJHXNWHUNYOXYIPCVPOUXNBBYYBKRYTATBUBSYUYLBEHHGREKKDOCPFABVLLILROAHRBAAVKDNYUNQGTFGFPFQCTKUDAOHRUXDZJVVZWRISMWGUQEKTCCKLHUDNQBNGNHMCVJAXJWCWMVSMZQBWIBLVRXQQQKJMSGQ

千秋此意

.style只能获取内联样式,设置样式也是内联的获取非内联的方法:/**  * div.currentStyle 兼容ie  * window.getComputedStyle firefox chrome等  * 以上返回一个CSSStyleDeclaration对象,即一个css样式的键值对集合  */ var width = (div.currentStyle || getComputedStyle(div, null))['width'];
随时随地看视频慕课网APP
我要回答