Quiero eliminar los nombres de "CASA" de todos estos divs:

<div><div style="padding:0!important;right:0!important;line-height:12px;display:block!important;font-size:8px!important;bottom:0!important;margin:0!important;text-align:center;position:absolute!important;opacity:0.8400000000000001!important;left:0!important;top:0!important;pointer-events:none">HOUSE</div></div>

<div><div style="padding:0!important;right:0!important;line-height:12px;display:block!important;font-size:8px!important;bottom:0!important;margin:0!important;text-align:center;position:absolute!important;opacity:0.8400000000000001!important;left:0!important;top:0!important;pointer-events:none">HOUSE</div></div>

<div><div style="padding:0!important;right:0!important;line-height:12px;display:block!important;font-size:8px!important;bottom:0!important;margin:0!important;text-align:center;position:absolute!important;opacity:0.8400000000000001!important;left:0!important;top:0!important;pointer-events:none">HOUSE</div></div>

<div><div style="padding:0!important;right:0!important;line-height:12px;display:block!important;font-size:8px!important;bottom:0!important;margin:0!important;text-align:center;position:absolute!important;opacity:0.8400000000000001!important;left:0!important;top:0!important;pointer-events:none">HOUSE</div></div>

Como puede ver, estos divs no tienen id o clase para encontrarlos. Lo he intentado con innerHTML o external pero no tuve suerte.

Me gustaría eliminar su texto y mantenerlos vacíos.

¿Alguna idea?

1
Sa51gitors 5 oct. 2021 a las 04:53

3 respuestas

La mejor respuesta

Puedes eliminar CASA de esta manera.

var divs = document.getElementsByTagName("div")
for (let div of divs) {
  if(div.innerHTML=="HOUSE")
  {
    div.innerHTML=""
  }
}
<div><div style="padding:0!important;right:0!important;line-height:12px;display:block!important;font-size:8px!important;bottom:0!important;margin:0!important;text-align:center;position:absolute!important;opacity:0.8400000000000001!important;left:0!important;top:0!important;pointer-events:none">HOUSE</div></div>

<div><div style="padding:0!important;right:0!important;line-height:12px;display:block!important;font-size:8px!important;bottom:0!important;margin:0!important;text-align:center;position:absolute!important;opacity:0.8400000000000001!important;left:0!important;top:0!important;pointer-events:none">HOUSE</div></div>

<div><div style="padding:0!important;right:0!important;line-height:12px;display:block!important;font-size:8px!important;bottom:0!important;margin:0!important;text-align:center;position:absolute!important;opacity:0.8400000000000001!important;left:0!important;top:0!important;pointer-events:none">HOUSE</div></div>

<div><div style="padding:0!important;right:0!important;line-height:12px;display:block!important;font-size:8px!important;bottom:0!important;margin:0!important;text-align:center;position:absolute!important;opacity:0.8400000000000001!important;left:0!important;top:0!important;pointer-events:none">HOUSE</div></div>
0
Yegor Sozonov 5 oct. 2021 a las 01:58

Si usa jQuery, puede usar el selector :contains:

$('div:contains(HOUSE)').text('')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div><div>HOUSE</div></div>

<div><div>HOUSE</div></div>

<div><div>HOUSE</div></div>

<div><div>HOUSE</div></div>

<div><div>:)</div></div>

De lo contrario, tendrá que seleccionar cada div, iterar a través de cada uno y eliminar el textContent para aquellos cuyo textContent contiene "HOUSE":

document.querySelectorAll('div').forEach(e => e.textContent = e.textContent.includes("HOUSE") ? '' : e.textContent)
<div><div>HOUSE</div></div>

<div><div>HOUSE</div></div>

<div><div>HOUSE</div></div>

<div><div>HOUSE</div></div>

<div><div>:)</div></div>
0
Spectric 5 oct. 2021 a las 01:55

Puedes usar xpath:

var xpath = "//div[text()='HOUSE']";
var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue

Como se sugiere aquí: Cómo obtener un elemento por innerText

0
iPaat 5 oct. 2021 a las 01:56