简体   繁体   English

如何在中间添加锚标记<p>使用 document.createElement(&quot;p&quot;) 时的元素

[英]How to add an anchor tag in the middle of a <p> element when using document.createElement("p")

I'm using document.createElement("p") to dynamically create elements.我正在使用document.createElement("p")动态创建元素。 I need to create an element <p></p> with an anchor tag in the middle of it so it creates a link word.我需要创建一个元素<p></p>在它的中间带有一个锚标记,以便它创建一个链接词。

I want to be able to inject the following html using Javascript using document.createElement("p") :我希望能够使用document.createElement("p")使用 Javascript 注入以下 html:

<p> This is a <a href="google.com">link</a> to google</p>

The word link should be an anchor tag surrounded by text from the <p> tag.单词 link 应该是一个锚标签,由<p>标签中的文本包围。

You can use innerHTML你可以使用innerHTML

let p = document.createElement('p');
p.innerHTML = "This is a <a href=\"http://www.google.com\">link</a> to google";

// append it
document.body.appendChild(p);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加类到使用document.createElement创建的元素? - How to add class to element created using document.createElement? 我们如何在 JavaScript 中为使用 document.createElement() 创建的元素添加焦点等事件? - How can we add events like on focus to an element created using document.createElement() in JavaScript? 在javascript中,如何使用document.createElement()在表中添加新行? - In javascript, how to add new rows in a table using document.createElement()? 如何创建一个 <input> 使用document.createElement()? - How to create an <input> using document.createElement()? 使用 document.createElement 创建父标签时,更改父标签中的 html 元素的 position - Changing html element's position in parent tag when created it with document.createElement 我可以在不同的元素上添加相同的document.createElement元素吗? - Can I add same document.createElement element on different element? 当锚标记下方有ap元素时,如何使锚标记填充li元素 - How can I make an anchor tag fill an li element when there is a p element right underneath the anchor tag 使用document.createElement onClick - Using document.createElement onClick 使用document.createElement()创建元素时,父元素是什么? - What is the parent when I create an element with document.createElement()? 将事件处理程序(带有参数)添加到使用document.createElement创建的元素 - Adding event handler (with parameters) to element created using document.createElement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM