gpt4 book ai didi

javascript - 单击链接后获取兄弟 ID

转载 作者:行者123 更新时间:2023-11-29 18:31:19 24 4
gpt4 key购买 nike

我使用以下代码,我希望能够从 <li> 中获取 ID 属性(帖子 ID)与 <a> 关联单击它时,但我不知道该怎么做。有人能帮我吗?

<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/

关于javascript - 单击链接后获取兄弟 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7979541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com