Getting BR-split text via DOM in JS / JQuery?

I am writing a greasemonkey script that parses a page with the following general structure:

<table>
<tr><td><center>
<b><a href="show.php?who=IDNumber">(Account Name)</a></b>
 (#IDNumber)
<br> (Rank)
<br> (Title)
<p>
<b>Statistics:</b>
<br>
<table>
<tr><td>blah blah etc.
</td></tr></table></center></table>

I am specifically trying to extract the (Title) part from this. However, as you can see, it is sent only by a tag <BR>, does not have its own identifier, is only part of the tag text <CENTER>, and this tag has a whole raft of other text associated with it.

Right now, what am I doing to get this, take the innerHTML of the Center tag and use the regular expression for it for /<br>([A-Za-z ]*)<p><b>Statistics/. This works well for me, but it seems like it's best to select this text.

... So, is there a better way? Or should I complain to the programmer on the site that he needs to make this text more accessible? :-)

+3
2

EDIT:

var title = $('table center').contents().filter(function() {
         if( this.nodeType == 3 && $.trim(this.data) != "") { //get only text nodes and filter out whitespace elements
           return true;
        }
      }).get(2); // get the 3rd text node 


    alert( title.data ); // alerts "(Title)
    title.data = "How to use jQuery"; // (Title) changes

:

node, . - nodeType 3, . , , , . ( , )

:

  $('table center').contents().filter(function() {
       if( this.nodeType == 3 && $.trim(this.data) != "") { //get only text nodes and filter out whitespace elements
           return true;
        }
      }).wrap('<p></p>') // make those text nodes paragraphs
      .end().filter('br')
        .remove(); // remove the brs

. jquery docs .contents()

+3

:

var result = $('table td:first-child > center > br:eq(1)').get(0)

alert(result.nextSibling.nodeValue);
+1

Source: https://habr.com/ru/post/1747078/


All Articles