以声明方式设置 HTML 链接的 jQuery.Data 属性

发布于 2024-12-06 19:22:47 字数 624 浏览 7 评论 0原文

假设我在数据网格或中继器内的行中有一个 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 属性添加值,但以声明方式执行此操作。

Assuming I have a HTML link in my rows inside a datagrid or repeater as such

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

Now also assuming that I have handled the click event for all my DoSomethings in jQuery as such

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

What is the correct technique for passing data to the click event that is dependent on the link clicked?

Without jQuery you would typically do something like this.

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

but this technique obviously doesn't work in the jQuery case.

Basically my ideal solution would somehow add values for to the jQuery.Data property for the link clicked but doing so declaratively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱你不解释 2024-12-13 19:22:47

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

http://api.jquery.com/data/#数据2

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

 $('.product-link').click(function (e) {
     alert($(this).data('productid'));
 });

Use HTML5 data- attributes. jQuery support is built-in for 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'));
 });
几度春秋 2024-12-13 19:22:47

您可以使用 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

});

You could use the attr() function.

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

});
韬韬不绝 2024-12-13 19:22:47

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

$(".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"
});

your question was not clear to me but may be this will help

$(".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"
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文