Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.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 向现有文本添加标记_Javascript_Html - Fatal编程技术网

Javascript 向现有文本添加标记

Javascript 向现有文本添加标记,javascript,html,Javascript,Html,如何向某些元素中已经存在的文本添加新标记。有没有比将文本复制到新元素然后从旧元素中删除更好的方法 在下面的示例中,文本Hello World!已存在于div中。目标是在文本周围添加h1 设myFunction=function{ 设d=document.getElementByIdtestId; 设h=document.createElementh1; h、 textContent=d.textContent; d、 textContent=; d、 儿童; }; .testClass{ 高度

如何向某些元素中已经存在的文本添加新标记。有没有比将文本复制到新元素然后从旧元素中删除更好的方法

在下面的示例中,文本Hello World!已存在于div中。目标是在文本周围添加h1

设myFunction=function{ 设d=document.getElementByIdtestId; 设h=document.createElementh1; h、 textContent=d.textContent; d、 textContent=; d、 儿童; }; .testClass{ 高度:100px; 保证金:自动; 宽度:200px; 边框样式:实心; 文本对齐:居中; } 你好,世界! 点击这里
这个问题可能会有多种意见

我的看法是,您使用domapi做得很好。但是,如果您了解.innerHTML的性能和安全含义,则可以执行以下操作:

设myFunction=function{ 设d=document.getElementByIdtestId; d、 innerHTML=+d.textContent+; }; .testClass{ 高度:100px; 保证金:自动; 宽度:200px; 边框样式:实心; 文本对齐:居中; } 你好,世界! 点击这里
课文是什么?它是在预定义区域中还是突出显示的文本?元素是什么?一个p?td?你想添加什么标签?请为您的问题添加更多细节