最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Creating a HTMLdivElement object in Javascript - Stack Overflow

programmeradmin1浏览0评论

Trying to turn a String into a HTMLDivElement object. I know how to create a string, I am just unsure how to create the HTMLDivElement.

For example, if I wanted to turn <div id="cardId" class="card c9"></div>

It would also be good to really understand what a HTMLDivElementObject is too.

EDIT:

I also know that there are Universal Attributes such as id(string), style(object), title(string) and className(string).

Below is my function and method. As you can see I try to create the toElement method. What I need is the method to return an HTMLDivElement object representing the Card. The example of what needs to be returned is as above <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 () {


    }
};

Trying to turn a String into a HTMLDivElement object. I know how to create a string, I am just unsure how to create the HTMLDivElement.

For example, if I wanted to turn <div id="cardId" class="card c9"></div>

It would also be good to really understand what a HTMLDivElementObject is too.

EDIT:

I also know that there are Universal Attributes such as id(string), style(object), title(string) and className(string).

Below is my function and method. As you can see I try to create the toElement method. What I need is the method to return an HTMLDivElement object representing the Card. The example of what needs to be returned is as above <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 () {


    }
};
Share Improve this question edited Oct 29, 2014 at 5:15 TheCrownedPixel asked Oct 29, 2014 at 5:10 TheCrownedPixelTheCrownedPixel 3691 gold badge7 silver badges18 bronze badges 3
  • 1 document.createElement("div"); – Developer Commented Oct 29, 2014 at 5:12
  • i cant get you,could you give source code – soundhiraraj Commented Oct 29, 2014 at 5:12
  • You'd need to deconstruct and reconstruct using @undefined answer and the js methods set attribute etc – Phix Commented Oct 29, 2014 at 5:19
Add a ment  | 

5 Answers 5

Reset to default 4
"toElement": function () {
  var dummy = document.createElement("div");
  dummy.innerHTML = this.toString();
  return dummy.children[0];
}

Figured it out guys. to set the Id, I needed to use .id, and .className for the class.

"toElement": function () {

    var s = this.card.getSuit(), r = this.card.getRank();

    var div = document.createElement('div');
    div.id = 'cardId';
    div.className = "card " +s+r;

    return div;

}

Assuming that you want to convert a string into an element, you can use RegEx to do this:

"toElement": function () {
    var string = '<div id="cardId" class="card c9"></div>'

    var e = /<(.+?)\s/g.exec(string)[1];
    var id = /id="(.+?)"/g.exec(string)[1];
    var cls = /class="(.+?)"/g.exec(string)[1];
    var element = document.createElement(e);
    element.hash = id;
    element.className = cls;
}
<!DOCTYPE html>
<html>
<head>
<script>
class Dartagnan extends HTMLDivElement{
constructor(align,text){
super(align);
this.textContent = text;
this.align = align;
this.style = "color:red;font-Weight:bold;font-Style:italic;";
this.contentEditable="true";
this.innerHTML += "<p style=color:blue;>aaaaaaaaaaa</p>";
}
};
customElements.define("test-div",Dartagnan,{extends:"div"});
</script>
</head>
<body>

<script>
var myDiv = new Dartagnan("right","xxxxxxxxxxxxxxxxxxxx");
document.body.appendChild(myDiv);
</script>
</body>
</html>

This is a working fiddle.

https://jsfiddle/zpa5bs9v/

<!DOCTYPE html>
<html>
<head>
<script>
customElements.define("sw-finn",class extends HTMLDivElement { connectedCallback(){
this.align = "right";
this.innerHTML = `<div contenteditable>xxxxx</div>`;
this.style.color = "red";
this.style.fontWeight = "bold";
}},{extends:"div"});
</script>
</head>
<body>
<div is='sw-finn'>aaaaa</div>
</body>
</html>

This is a working fiddle.

https://jsfiddle/7qzya0kc/

发布评论

评论列表(0)

  1. 暂无评论