2

假设我在数据网格或中继器内的行中有一个 HTML 链接

<a href="javascript:void(0);" class="DoSomething">DoSomething</a>

现在还假设我已经在 jQuery 中处理了我的所有 DoSomethings 的点击事件

$(".DoSomething").click(function (e) {
    //Make my DoSomethings do something
});

将数据传递给取决于单击的链接的单击事件的正确技术是什么?

如果没有 jQuery,你通常会做这样的事情。

<a href="javascript:void(0);" class="DoSomething" onclick="DoSomething('<%# Eval("SomeValue") %>');">DoSomething</a>

但这种技术显然不适用于 jQuery 案例。

基本上,我理想的解决方案会以某种方式为单击的链接的 jQuery.Data 属性添加值,但以声明方式这样做。

4

3 回答 3

4

使用 HTML5 数据属性。jQuery 支持内置于 1.4.3+

http://api.jquery.com/data/#data2

 <a href="#" class="product-link" data-productid="77">click here</a>

 $('.product-link').click(function (e) {
     alert($(this).data('productid'));
 });
于 2011-09-27T04:47:03.273 回答
2

您可以使用 attr() 函数。

http://api.jquery.com/attr/

$("#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

});
于 2011-09-27T04:42:40.433 回答
2

你的问题我不清楚,但这可能会有所帮助

$(".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"
});
于 2011-09-27T04:46:01.353 回答