简体   繁体   English

如何访问当前html元素的某些属性值?

[英]How can I access current html elements certain property value?

This is my js/jquery code in which I am trying to access the current button that was clicked, with class of delete, and then get the property data-id's value. 这是我的js / jquery代码,我试图在其中访问带有删除类的当前单击的按钮,然后获取属性data-id的值。 When I console.log the string that I hope to have it says undefined? 当我console.log我希望它具有未定义的字符串? Any suggestions on what I am doing wrong? 关于我在做什么错的任何建议吗?

$(".delete").click(function(){
    var id = $(this).prop("data-id");
    console.log(id);
});

Since data-* is an attribute you need to use attr() 由于data-*是属性,因此您需要使用attr()

var id = $(this).attr("data-id"); 

or you can get data-* attribute value using data() 或者您可以使用data()获得data-*属性值

var id = $(this).data("id");

Try 尝试

$('.delete').on('click',function(){
 var id=  $(this).attr('data-id');
 console.log(id);
})

尝试attr()方法。

var id = $(this).attr("data-id");

The shortest would be: 最短的是:

$(".delete").on('click', function(){
    var id = $(this).data("id");
    console.log(id);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问html元素的属性? - How can I access a property of an html element? 如何访问动态推送到html中的html元素? - How can I access html elements that were dynamically pushed into html? 如何为数组中的第一个元素返回属性值,如果没有元素则返回0? - How can I return a property value for the first element in an array or 0 if there are no elements? 如何使用javascript访问HTML中的未命名元素? - How can I access unnamed elements from an HTML using javascript? 如何在 Vue3 中访问 object 的“_value”属性 - How can I access the "_value" property of the object in Vue3 如何检查 HTMLCollection 中的所有元素是否都包含某个属性的特定值? - How to check if all elements in an HTMLCollection contain a certain value for a property? 如何根据某个属性的值将元素数组拆分为组? - How to split an array of elements into groups by the value of a certain property? 我如何将类添加到特定的html元素(如果它们包含特定值) - How do I add a class to specific html elements, If they contain a certain value 如何更改对象属性IF的值,并且仅在该值满足特定条件的情况下才能更改? (JavaScript) - How can I change the value of an objects property IF and only IF that value meets a certain condition? (Javascript) 如何在特定条件下停止 object 属性访问链? - How can I stop an object property access chain under certain conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM