我有这样的链接:
<li><a href="#" data-value="Codes">Get codes</a></li>
<li><a href="#" data-value="Product">Get products</a></li>
我怎样才能做到当我点击链接时调用一个函数来传递数据值中包含的值?
function refreshGrid(value) { }
最佳答案
尝试以下操作:
$('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
});
关于javascript - 调用 onClick 函数并将元素的 data-* 属性中的值传递给它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10007078/