试图将字符串转换为 HTMLDivElement 对象。我知道如何创建一个字符串,我只是不确定如何创建 HTMLDivElement。
例如,如果我想转<div id="cardId" class="card c9"></div>
真正了解什么是 HTMLDivElementObject 也很好。
编辑:
我也知道还有id(string)、style(object)、title(string)和className(string)等Universal Attributes。
下面是我的功能和方法。如您所见,我尝试创建 toElement
方法。我需要的是返回表示 Card 的 HTMLDivElement 对象的方法。需要返回的例子如上<div id="cardId" class="card c9"></div>
.
var cardProto = {
// creates the HTML string representing this element
"toString": function (){
var s = this.card.getSuit(), r = this.card.getRank();
var t = "<div class=\"card ";
t += s !== undefined ? s + r : "down";
t += "\" id=\""+ this.id + "\"></div>\n";
return t;
},
"toElement": function () {
}
};
最佳答案
"toElement": function () {
var dummy = document.createElement("div");
dummy.innerHTML = this.toString();
return dummy.children[0];
}
关于javascript - 在 Javascript 中创建 HTMLdivElement 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26623150/