簡體   English   中英

如何在jQuery表達式中訪問當前元素的屬性?

[英]How to access the current element's attribute in a jQuery expression?

我想在每個選定的元素上調用一個函數:

$('any valid selector').existingFunction({ p1:<myAttributeValueForTheCurrentElement> });

我試過了:

$('any valid selector').existingFunction({ p1: this.attr('myAttributeValueForTheCurrentElement') });

但顯然這是指HTMLDocument,因為我收到了錯誤消息:'對象#沒有方法'attr''

this與外部范圍有關。

您的代碼等同於以下內容:

var obj = { p1: this.attr('myAttributeValueForTheCurrentElement') };

$('any valid selector').existingFunction(obj);

您將需要迭代集合中的元素。

$('selector').each(function(){

   var options = { p1: $(this).attr('myAttributeValueForTheCurrentElement') };

   $(this).existingFunction(options);

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM