简体   繁体   English

使用javascript改变<div>的内容

[英]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 . <div>没有idclass时,如何使用javascript更改<div>的内容。

I know that using something like document.getElementById('foo').innerHTML = 'Your content'; 我知道使用像document.getElementById('foo').innerHTML = 'Your content';这样的东西document.getElementById('foo').innerHTML = 'Your content'; Could have been used if the <div> had an id. 如果<div>有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"; 我偏爱使用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 不,还有其他div,但这是唯一一个内容Boo

If that case, you can find by :contains 如果是这种情况,你可以通过:contains找到

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

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

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