我使用以下代码,我希望能够从<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 = '???';
});
});
感谢。
答案 0 :(得分:2)
答案 1 :(得分:1)
您可以使用:
var post_id = $(this).parent().attr("id");
如果你需要获得以下(兄弟姐妹)&lt; li&gt;,你也可以这样做:
var next_id = $(this).parent().next().attr("id");
答案 2 :(得分:1)
你的标记中带有post id的li:s实际上不是兄弟姐妹而是父母。
应该这样做:
var post_id = $(this).parent()。attr('id'));