简体   繁体   中英

How do I give an id to an appended element?

The one I'm talking about is one where I have the code appear literally, a string. How do I combine it with actual code, like <div id="thing">string goes here</div> ?
I've tried making the div and id part of the content inside the (' ') but it doesn't work. When I do that, the content never gets appended.

Here's the code:

$('#placeToAppendTo').append
('&lt;br&gt;&lt;div&gt;&lt;input&nbsp;type="text"&nbsp;class="textfield"&nbsp;id="text'+textClicked+'"&lt;/div&gt;');

To get the HTML you presented, you would do something like this:

var elem = document.createElement('div');
elem.id = "thing";
elem.appendChild(document.createTextNode("string goes here"));

I hope this helps, since your question isn't particularly clear.

或在jQuery中:

$('<div />', {id:'thing', text:'string goes here'});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM