简体   繁体   中英

Using javascript to alter content of a <div>

How do you alter the content of a <div> using javascript when the <div> has no id or class .

I know that using something like document.getElementById('foo').innerHTML = 'Your content'; Could have been used if the <div> had an id. But in my case the content I want to alter is like this

<div unselectable="on" style="position: absolute; padding: 2px; top: 0px; left: 1px; background-color: rgb(255, 102, 0); color: white;">BOO</div>

I am partial to using document.getElementsByTagName("div")[0].innerHTML = "BOO"; to find it but how do I then proceed to modify its content? Also is this a good way to doing things?

No there are other div too but that is the only one with that content Boo

If that case, you can find by :contains

$("div:contains('BOO')").html('hello');

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