简体   繁体   中英

Insert html (unicode text) after an element

I have an element is called divElement :

<div ....>

</div>

and I have p element is called pElement :

<p ....>

</p>

I did:

divElement.appendChild(pElement);

and I got new element:

<div ....>
    <p ....>

    </p>
</div>

now I want to add this unicode text (▽) after the p element in order to get:

<div ....>
    <p ....>

    </p>
    &#9661;
</div>

I tried:

divElement.innerHTML = "&#9661;";

but I got:

<div ....>
    &#9661;
    <p ....>

    </p>
</div>

any help appreciated!

You could do this:

divElement.appendChild(document.createTextNode("▽"))

Or, if you don't want to include the literal character:

var charDiv = document.createElement('div')
charDiv.innerHTML = "&#9661;"
document.body.appendChild(charDiv)

Simply concatenate the sign with the innerHTML of the div.

HTML :

<div id="divElement">
    <p id="pElement">
        Hello
    </p>
</div>

javaScript :

var element = document.getElementById("divElement");
element.innerHTML += "&#9661";

Demo

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