将html字符串添加到DOM作为元素 [英] Add html string to DOM as element

查看:501
本文介绍了将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;

这篇关于将html字符串添加到DOM作为元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆