单击链接后获取兄弟姐妹的ID

Get the ID of a sibling after a link is clicked

本文关键字:ID 兄弟姐妹 获取 链接 单击      更新时间:2023-09-26

我使用以下代码,我希望能够在单击<a>时从与之关联的<li>(post ID(中获取ID属性,但我不知道如何做到这一点。有人能帮我吗?

<li id="custom_latest_news-5" class="widget custom_widget_latest_news">
    <div class="widget-title">
        <a href="http://test.dynedrewett.com/news-and-events/">Latest news</a>
    </div>
    <div class="widget-content">
        <ul class="link-list ajax-links">
            <li id="post-5521"><a href="http://mydomain.com/uk-news/last-call-for-self-assessment-tax-forms/">Last call for self-assessment tax forms</a></li>
            <li id="post-5523"><a href="http://mydomain.com/uk-news/young-drivers-gambling-on-no-insurance/">Young drivers gambling on no insurance</a></li>
            <li id="post-5520"><a href="http://mydomain.com/uk-news/tenants-forced-to-abandon-pets/">Tenants forced to abandon pets</a></li>
        </ul>
    </div>
</li>

这是我目前拥有的JS-

/**
 * Intercept a click for page refresh and run the page refresh JS
 */
$(function(){
    $('.ajax-links').delegate('li a', 'click', function(e){
        /** Prevent the click from doing anything */
        e.preventDefault();
        /** Get the post ID */  
        var post_id = '???';
    });
});

谢谢。

我相信你需要

var post_id = $(this).parent().attr('id');

示例:http://jsfiddle.net/FwqGT/

您可以使用:

var post_id = $(this).parent().attr("id");

如果您需要获得以下(兄弟(<li>,你也可以做:

var next_id = $(this).parent().next().attr("id");

标记中带有post-id的li:s实际上不是兄弟,而是父。

这应该做到:

var post_id=$(this(parent((.attr('id'((;