将html字符串添加到DOM作为元素
问题描述:
总有没有创建字符串并添加到DOM的信息?并使用Javascript来理解字符串中的元素?
Is there anyway to create a string and add to the DOM? And having Javascript to understand the elements in the string?
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);
我需要在str中向DIV myDiv添加多个标签
I need to add several tags in str to the DIV myDiv
我不需要使用jQuery,因为脚本不会加载jQuery
谢谢。
I need NOT to use jQuery since the script will not load jQuery Thanks.
答
innerHTML
属性不是函数,您应该像这样分配:
The innerHTML
property is not a function, you should assign it like this:
var bmdiv = document.createElement('div');
bmdiv.setAttribute('id', 'myDiv');
var str = "<b>aa</b>";
bmdiv.innerHTML = str;