Domanda

Ho usato la funzione:

document.getElementsByTagName('strong')

per ottenere tutto il testo in una pagina con quel tipo di formattazione. L'HTML è simile a:

<td align="center" valign="bottom"><H1><font size="+4"><strong>TEXT_HERE</strong></font> <br>

Vorrei cambiare " TEXT_HERE " forse qualcos'altro o rimuoverlo tutto insieme. Come potrei farlo?

Grazie in anticipo per il tuo aiuto :)

È stato utile?

Soluzione

Con un ciclo for?

var strongElems = document.getElementsByTagName('strong');
var wantToHide  = true || false;

for (var i=0; i<strongElems.length; i++)
{
  var thisElem = strongElems[i];
  if (wantToHide)
  {
    thisElem.style.display = "none"; // hide it
  }
  else
  {
    thisElem.textContent = "something else"; // change it
  }
}

Altri suggerimenti

// ==UserScript==
// @name           MyScript
// @namespace      http://example.com
// @description    Example
// @include        *
//
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
// ==/UserScript==

var shouldHide = false;

$('strong').each(function() {
  if(shouldHide) {
    $(this).hide();
  } else {
    $(this).text("New Text");
  }
});

Quando volevo cambiare del testo in una pagina Web, ho basato la mia soluzione Greasemonkey su DumbQuotes . Mi è piaciuto poter facilmente definire coppie di sostituzione in un elenco:

replacements = {
  "old": "new",
  "foo": "bar",
};

Detto questo, controllerò anche la soluzione di Tomalak. Grazie per aver pubblicato questa domanda.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top