[英]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.