0

誰でも知っていますか、属性を設定した最初のロード時に div 要素がright:0ありbottom:0ます ; 取得したいのですstyle.leftが、戻りNaNます。それで、それを取得する方法、または別の方法はありますか?

<div id="myDiv">Sample</div>
<script type="text/javascript">
    var setMe = document.getElementById("myDiv");
    setMe.setAttribute("style", "background-color: red;
        position: absolute;
        width: 670px;
        height: 370px;
        right: 0;
        bottom: 0;");
    alert(setMe.style.left)
</script>
4

3 に答える 3

1

getComputedStyle()を使用して試してください: ( Demo )

var setMe=document.getElementById("myDiv");

setMe.setAttribute("style", "background-color:red;position:absolute;width:670px;height:370px;right:0;bottom:0;");

alert(window.getComputedStyle(setMe).left);
于 2013-07-05T03:04:08.607 に答える
0

offsetLeft属性を使用します。

<html>
<head></head>
<body>
<div id="myDiv">Sample</div>
<script type="text/javascript">
    var setMe=document.getElementById("myDiv");
    setMe.setAttribute("style", "background-color:red;position:absolute;width:670px;height:370px;right:0;bottom:0;");
    alert(setMe.offsetLeft);
</script>
</body>
</html>
于 2013-07-05T03:03:18.030 に答える
0

次の 3 つのオプションがあります。

style property // --> Returns an object that represents the element's style attribute (setted when parsed)

getAttribute // --> returns the value of the named attribute on the specified element.

computedStyle // -->  gives the final used values of all the CSS properties of an element.

すべての例については、このフィドル(デモ) をご覧ください。

また:

于 2013-07-05T03:05:43.463 に答える