简体   繁体   中英

Adding programmatically a span tag with an anchor inside innertext

I have created a span element using:

ele= document.createElement(span);
ele.innerText = "bla bla (<a href='https://www.pepe.com'> ble ble </a>) blu blu";
divele.appendChild(ele); #divele is an div in the page

The problem is the tag is not showed as a link, it is showed literally.

Should be innerHTML instead of innerText (since the sting contains HTML in it). The span should be enclosed with single quotes ( '' ) or double quotes ( "" ).

 var ele= document.createElement('span'); ele.innerHTML = "bla bla (<a href='https://www.pepe.com'> ble ble </a>) blu blu"; var divele = document.getElementById('divele'); divele.appendChild(ele); 
 <div id="divele"><div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM