开发者

:last isn't working

开发者 https://www.devze.com 2023-03-07 00:45 出处:网络
jQuery(\".xg_module_body .postbody a:last\").addClass(\"continue\"); Is there something wrong w/ that code? The class doesn\'t get added for some reason.

jQuery(".xg_module_body .postbody a:last").addClass("continue");

Is there something wrong w/ that code? The class doesn't get added for some reason.

I'm trying to add a link to just the last a in .postbody. I've tried :last-child and it adds a class to both a tags. So why wouldn't :last work?

<div class="xg_module_body">
    <h3>
        <span>
            <a href="link">
                <img src="image url">
            </a>
        </span>
        <a href="link">Lorem Ipsum</a>
    </h3>
    <div class="postbody">
        <p>
            <a href="link"><img src="image url"></a>
        </p>
        <p>Lorem Ipsum</p>
        <a href="link">Continue</a>
    </div>
    <p class="small">
        <a href="link">Lorem Ipsum</a>
    </p>
    <p>
        &l开发者_C百科t;a href="link">Lorem Ipsum</a>
    </p>
</div>


As seen here, it does work : http://jsfiddle.net/B9CwH/

I'm betting your selector is wrong. The snippet you pasted doesn't have a .xg_module_body

<div class="postbody">
     <p>
          <a href="link"><img src="image"></a>
     </p>
     <p>title</p>
     <a href="link">Continue</a>
</div>

$(".postbody a:last").addClass("continue");


working perfect:

see here :

http://jsfiddle.net/xkeshav/q5jK2/

other way : try with filter:

$('.postbody').filter(':last').addClass("continue");

DEMO

0

精彩评论

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