簡體   English   中英

當前元素的jQuery屬性值

[英]Jquery attribute value for current element

我試圖找到被點擊元素的屬性值。 所有元素都是動態填充的。

我無法獲得屬性的值。 可能是什么問題?

<span title="cancel work" class="cancel" myid="111"><i class="fa fa-stop"></i> Stop work</span>

$(document).on('click','.cancel',(function(){
    alert(this.attr("myid"));
}));

這是我收到的錯誤消息

對象不支持屬性或方法“ attr”

myid不是有效的HTML屬性。 大多數約定都通過實現data屬性來使用此類自定義屬性。 例如data-myid

<span title="cancel work" class="cancel" data-myid="111"><i class="fa fa-stop"></i> Stop work</span>

$(document).on('click','.cancel',(function(){
  alert($(this).data("myid"));
}));

注意更改:

  • 我在span使用data-myid
  • 我通過使用jQuery的data方法獲得其價值。

暫無
暫無

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

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