开发者

jQuery, Get contents of a tag after a class

开发者 https://www.devze.com 2023-03-28 05:54 出处:网络
I need to get contents of a \"b\" tag and unfortunately I cannot change the html. <div class=\"mw_error\">

I need to get contents of a "b" tag and unfortunately I cannot change the html.

<div class="mw_error">

    // Sometimes "b" tags are inserted here and sometimes not

    <span class="loseTe开发者_如何学Goxt">
       <b> You Lost </b>
    </span>

    Text 1
    <b> Need to access this value </b>
    Text 2
    <b> Need to access this value </b>

</div>

I can get the correct contents of the "b" tags as long as those two "b".

The problem is that sometimes there are "b" tags inserted between the "span class lose Text" depending on the message returned.

I was wondering if it is possible to use jQuery to select the "b" tags after the span class "loseText". I have looked at the .after function but cant seem to get it to work.

Any help on this would be much appreciated :)

I forgot to say I need to only select the first two "b" tags after the span class.


try:

$('.loseText').nextAll('b').slice(0, 2);

Edited to only select 2 (maximum)


Try this

var $b = $('.loseText ~ b');
if ($b.length) {
    // Do stuff here
}

DEMO

To limit it to the 1st two tags, use:

var $b = $('.loseText ~ b:lt(2)');

See the demo for that.


you can also use:

$('.loseText').children('b')


Try this

$(".loseText").nextAll("b");


try something like:

$('.mw_error > b').each(...);

See the example. jsFiddle.


I think this is what you need:

$("div.mw_error").children("b");

This shold get the b tags which are not within the span.loseText.

0

精彩评论

暂无评论...
验证码 换一张
取 消