3

我有这样的链接:

<li><a href="#" data-value="Codes">Get codes</a></li>
<li><a href="#" data-value="Product">Get products</a></li>

我怎样才能做到这一点,以便当我单击链接时调用一个函数,传递数据值中包含的值?

function refreshGrid(value) { }
4

3 回答 3

4

尝试以下操作:

$('li > a[href="#"][data-value]').click(function (e) { // On click of all anchors with a data-value attribute and a href of # that are immediate children of li elements (nb this selector can be modifed to suit your needs)
  e.preventDefault(); // prevent the default (navigate to href)
  refreshGrid($(this).data('value')); // call your refreshGrid function with the value contained in the 'data-value' attribute
});
于 2012-04-04T08:03:13.917 回答
1
var linkValue = $("li a").attr("data-value");

编辑:更好的例子

$("li a").click(function () {
    var myDataValue = $(this).attr("data-value");
    refreshGrid(myDataValue);
});
于 2012-04-04T08:02:33.537 回答
1

这是一个快速示例

$('ul').on('click','a[data-value]',function(){
    refreshGrid(this.getAttribute('data-value'));
    return false;
});
于 2012-04-04T08:07:34.853 回答