2017-07-31 32 views
-1

我想从列表中的链接中获取数据属性,当我点击它时。从ul列表中的链接获取数据属性

$(document).on('click', "ul.pagination li a", function(e) 
{ 
    e.preventDefault(); 
    var page = $(this).attr("page"); 
    var article_id = getUrlParameter('aid'); 
    alert(page); 
    $('.comments').load('page', {'type':'reload', 'article_id':article_id, 'page':page}, function() { 
     $('.comments').scrollMinimal(); 
     $('.comments').highlight(); 
    }); 
}); 

名单应该是这样的:

<div class="fnone"> 
    <ul class="pagination"> 
    <li><a data-page="2" href="testpage=2#comments">2</a></li> 

我的问题是, “页” 总是undefined。不完全确定我在这里出错的地方。

+0

使用'$(本).attr( “数据页”);' – guradio

+0

的document.getElementById (ID).getAttribute( “数据”); – Rigidity

回答

1
  1. 使用.attr("data-page"),因为这是属性的名称

$(document).on('click', "ul.pagination li a", function(e){ 
 
    e.preventDefault(); 
 
    var page = $(this).attr("data-page"); 
 
    alert(page) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
A list would look like this: 
 

 
<div class="fnone"> 
 
    <ul class="pagination"> 
 
    <li><a data-page="2" href="testpage=2#comments">2</a></li> 
 
    </ul> 
 
    </div>

+1

Doh,简单的错误呃?谢谢 :) – NaughtySquid