将对象附加到文档后获取对象

时间:2019-05-16 02:11:41

标签: javascript

我现在已经建立了一个视频播放器对象,该对象在video中有一个div。它还存储info。我创建了这个对象,然后appendChild这个对象。那么如何在info被使用后重新获得appendChild

function Video_Player() {
    this.box = document.createElement("div");
    this.player = document.createElement("video");
    this.info = "-----info-----";

    this.Build = function() {
        this.box.style = "background-color:red; width:200px; height:200px;";
        this.player.style = "width:200px; height:150px;";
        this.player.controls = true;
        this.box.appendChild(this.player);
    };

    this.getBuild = function() {
        return this.box;
    };
}


var content = document.getElementById("content");

// Build Object
var player = new Video_Player();
player.Build();
content.appendChild(player.getBuild());
// Print id
console.log("id before appendChild: " + player.info);

// re get the 'object' id
console.log("id after appendChild: " + content.querySelector("div").info);
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div id="content"></div>
</body>
</html>

0 个答案:

没有答案