javascript - nextElementSibling freezing firefox -
i'm trying search specific text highlight link, when found. because links not within h4 tags don't know how solve it.
html code<h4>title p</h4> ... <h4>title x</h4> <p class="item"> <a href="url" target="_new">abc</a> </p> <div class="c"> <p class="item"> <a href="url" target="_new">efg</a> </p> <div class="c"> <p class="item"> <a href="" target="_new">mno</a> </p> <div class="c"> <p class="item"> <a href="url" target="_new">xyz</a> </p> <h4>title z</h4> ...
the text wanna find repeats under others h4 tags, want highlight ones within title x.
so here code:// @grant gm_addstyle ... var h4 = document.getelementsbytagname("h4"); (var = 0; < h4.length ; i++) { if (h4[i].textcontent.indexof("title x") != -1) { var links = h4[i].nextelementsibling; while (links && links.nodename != "h4") { if (links.textcontent.indexof("efg") != -1){ links.setattribute("id","color"); } if (links.textcontent.indexof("mno") != -1){ links.setattribute("id","color"); } } } } gm_addstyle ( ' \ #color { \ background-color: #ffd65e; \ } \ ' );
if remove while, browser not freeze.
after reply working code was:var links = h4[i]; while ( (links = links.nextelementsibling) && (links.nodename != "h4") ) { if (links.textcontent.indexof("efg") != -1) { links.setattribute("id", "color"); } ... }
this seems expected behavior of code. see next 2 lines.
var links = h4[i].nextelementsibling; while (links && links.nodename != "h4") {
you set links
element next current h4
element (in case, first one, never continues), , repeatedly checks if element truthy , not h4
element. in body of while
statement links
re-assigned, simple same check on , on again, causing hanging script.
i'm not sure trying script, perhaps intended link iterate on sibling elements?
var links = h4[i]; while ( (links = links.nextelementsibling) && (links.nodename != "h4") ) {
javascript greasemonkey
No comments:
Post a Comment