[英]Add html string to DOM as element
Is there anyway to create a string and add to the DOM? 反正有没有创建一个字符串并添加到DOM? And having Javascript to understand the elements in the string?
并使用Javascript来理解字符串中的元素?
I tried the below and 4th line gives error:
var bmdiv = document.createElement('div');
bmdiv.setAttribute('id', 'myDiv');
var str = "<b>aa</b>";
bmdiv.innerHTML(str);
I need to add several tags in str to the DIV myDiv 我需要在str中为DIV myDiv添加几个标签
I need NOT to use jQuery since the script will not load jQuery Thanks. 我不需要使用jQuery,因为脚本不会加载jQuery谢谢。
The innerHTML
property is not a function, you should assign it like this: innerHTML
属性不是函数,您应该像这样分配它:
var bmdiv = document.createElement('div');
bmdiv.setAttribute('id', 'myDiv');
var str = "<b>aa</b>";
bmdiv.innerHTML = str;
Try 尝试
bmdiv.innerHTML = str;
Another way to do this is to manually create the DOM structure for each of the tags, then append them into the div. 另一种方法是为每个标记手动创建DOM结构,然后将它们附加到div中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.