JQuery Select text within br and td in table

2019-07-27 12:30发布

问题:

I'm trying to get the JQuery right to select the text from the following html structure:

<TABLE class=ms-formtable>
<TBODY>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR>
<TD><H3><NOBR>Select this text</NOBR></H3></TD>
<TD></TD>
</TR>
</TBODY>
</TABLE>

回答1:

simple selector

$('nobr').text();

more explicit selector

$('table.ms-formtable > tbody > tr > td > h3 > nobr').text();

An example to play with



回答2:

There is a number of ways of doing that. Is this the only table with that class name? My guess is $('table.ms-formtable nobr').text(), but it depends on the rest of the HTML, because this could select more than one element's text.



回答3:

Could you perhaps use an ID to specify your H3 tag and therefore be able to select it using something like $('#textToSelect').text().

That way you could be absolutely sure which text you were targeting on the page.