2017-07-31 31 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