如何从createElement获取X(' img')

时间:2015-11-06 05:37:04

标签: javascript css html5

如何从标签document.createElement获取X(左)位置(' img');

var block00 = document.createElement("img");
block00.src = "images/sep1.png";

你能用:

if (block00.getBoundingClientRect) {
    var doc2 = block00.getBoundingClientRect();
    var X = doc2.left;
    console.log(X);
}

1 个答案:

答案 0 :(得分:0)

首先你必须appendChild();

        var block02 = document.createElement("img"); // ADD SEPERATION BLOCK(BLOCK HOLE)
        block02.src = "images/sep1.png"; // ADD SEPERATION BLOCK(BLOCK HOLE)
        document.getElementById("land01").appendChild(block02); // APPEND CHILD(BLOCK HOLE)

        ///////////////////////////////////////////////////////////////////////////
        //
        // BLOCK02 GET X POS OF ADDED BLOCK 

        if (getX1 == 0) { //////////////////////////////////////////// SET (BLOCK HOLE) X
            var doc2 = block02.getBoundingClientRect();
            gX1 = doc2.left;
            getX1 = 1;
        }