Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用innerHTML附加到对象_Javascript - Fatal编程技术网

Javascript 如何使用innerHTML附加到对象

Javascript 如何使用innerHTML附加到对象,javascript,Javascript,有没有一种方法可以使用.innerHTML向DOM对象添加信息而不替换其中存在的内容 例如:document.getElementById('div').innerHTML='stuff' 将返回stuff 然后是一个类似的调用:document.getElementById('div').innerHTML='And things' 将div设置为stuff and thingsthe.the.将其更改为: document.getElementById.innerHTML('div') =

有没有一种方法可以使用.innerHTML向DOM对象添加信息而不替换其中存在的内容

例如:
document.getElementById('div').innerHTML='stuff'

将返回
stuff

然后是一个类似的调用:
document.getElementById('div').innerHTML='And things'

将div设置为
stuff and things
the.

the.

将其更改为:

document.getElementById.innerHTML('div') = 
    document.getElementById.innerHTML('div') + ' and things';
ol'x=x+1串联样式

虽然我不认为“document.getElementById.innerHTML('div')”是正确的语法?您的意思是“document.getElementById('divid').innerHTML”吗?

将其更改为:

document.getElementById.innerHTML('div') = 
    document.getElementById.innerHTML('div') + ' and things';
ol'x=x+1串联样式

虽然我不认为“document.getElementById.innerHTML('div')”是正确的语法?你是说“document.getElementById('divid').innerHTML”吗?

也许:

document.getElementById('div').innerHTML = document.getElementById('div').innerHTML + ' and things'
也许:


你的例子中有错误。但你可以这样做:

var myDiv = document.getElementById('div');

myDiv.innerHTML = myDiv.innerHTML + ' and things';

你的例子中有错误。但你可以这样做:

var myDiv = document.getElementById('div');

myDiv.innerHTML = myDiv.innerHTML + ' and things';
为什么不试试呢

document.getElementById('div').innerHTML += ' and things';
注意“+”

为什么不试试

document.getElementById('div').innerHTML += ' and things';
请注意您可以使用的“+”

insertAdjacentHTML()将指定的文本解析为HTML或XML,并将生成的节点插入到DOM树的指定位置。它不会重新分析正在使用它的元素,因此不会损坏元素中的现有元素。这样,避免了额外的序列化步骤,使其比直接的innerHTML操作快得多

如果您想将
内容
附加到此
元素
的末尾,您可以这样做

const div = document.getElementById("div");
div.insertAdjacentHTML('beforeend', 'stuff');;
你可以用

insertAdjacentHTML()将指定的文本解析为HTML或XML,并将生成的节点插入到DOM树的指定位置。它不会重新分析正在使用它的元素,因此不会损坏元素中的现有元素。这样,避免了额外的序列化步骤,使其比直接的innerHTML操作快得多

如果您想将
内容
附加到此
元素
的末尾,您可以这样做

const div = document.getElementById("div");
div.insertAdjacentHTML('beforeend', 'stuff');;

回答:回答:+1用于识别OP源中存在错误。+1用于识别OP源中存在错误。这太棒了!非常感谢。这太棒了!非常感谢。这将删除附加到div内existen元素的所有事件这将删除附加到div内existen元素的所有事件这给了我一个
错误类型错误:“document.getElementById(…)
为null”在TypeScription中使用时这给了我一个
错误类型错误:“document.getElementById(…)
在typescript中使用时为空