[英]Declaratively setting jQuery.Data property for a HTML link
假設我在數據網格或轉發器中的行中有HTML鏈接
<a href="javascript:void(0);" class="DoSomething">DoSomething</a>
現在還假設我已經在jQuery中處理了所有我的DoSomethings的click事件
$(".DoSomething").click(function (e) {
//Make my DoSomethings do something
});
將數據傳遞給依賴於單擊鏈接的click事件的正確技術是什么?
沒有jQuery,你通常會做這樣的事情。
<a href="javascript:void(0);" class="DoSomething" onclick="DoSomething('<%# Eval("SomeValue") %>');">DoSomething</a>
但是這種技術顯然不適用於jQuery案例。
基本上我的理想解決方案會以某種方式為點擊鏈接的jQuery.Data屬性添加值,但是以聲明方式這樣做。
使用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'));
});
您可以使用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
});
你的問題對我來說並不清楚,但可能會有所幫助
$(".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"
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.