Javascript 声明式设置HTML链接的jQuery.Data属性
假设我在datagrid或repeater中的行中有一个HTML链接Javascript 声明式设置HTML链接的jQuery.Data属性,javascript,jquery,asp.net,repeater,declarative,Javascript,Jquery,Asp.net,Repeater,Declarative,假设我在datagrid或repeater中的行中有一个HTML链接 <a href="javascript:void(0);" class="DoSomething">DoSomething</a> 向依赖于所单击链接的单击事件传递数据的正确方法是什么 如果没有jQuery,您通常会这样做 <a href="javascript:void(0);" class="DoSomething" onclick="DoSomething('<%# Eval("So
<a href="javascript:void(0);" class="DoSomething">DoSomething</a>
向依赖于所单击链接的单击事件传递数据的正确方法是什么
如果没有jQuery,您通常会这样做
<a href="javascript:void(0);" class="DoSomething" onclick="DoSomething('<%# Eval("SomeValue") %>');">DoSomething</a>
但是这种技术显然不适用于jQuery
基本上,我的理想解决方案是以某种方式为已单击但以声明方式执行的链接的jQuery.Data属性添加值 您可以使用attr()函数
我不清楚你的问题,但这可能会有所帮助
$(".DoSomething").click(function (e) {
//Make my DoSomethings do something
$(this).data("key","value");
//later the value can be retrieved like
var value=$(this).data("key");
console.log(value);// gives you "value"
});
使用HTML5数据属性。jQuery支持内置于1.4.3中+
$('.product link')。单击(函数(e){
警报($(this.data('productid'));
});
$("#Something").attr("your-value", "Hello World");
$("#Something").click(function (e) {
//Make my DoSomethings do something
var value = $(this).attr("your-value");
alert(value); // Alerts Hello World
});
$(".DoSomething").click(function (e) {
//Make my DoSomethings do something
$(this).data("key","value");
//later the value can be retrieved like
var value=$(this).data("key");
console.log(value);// gives you "value"
});
<a href="#" class="product-link" data-productid="77">click here</a>
$('.product-link').click(function (e) {
alert($(this).data('productid'));
});