简体   繁体   English

如何访问放置在不同标签中的自定义 data-* html5 属性,通常在 jquery 中?

[英]how to access custom data-* html5 attribute placed in different tag, commonly in jquery?

I'm trying to create a in-place editor where I'm fetching the data by putting the custom data attributes in different tags.我正在尝试创建一个就地编辑器,通过将自定义数据属性放在不同的标签中来获取数据。 I want to access the data in jquery selector and update the value accordingly.我想访问 jquery 选择器中的数据并相应地更新值。 Please guide me with the common syntax used to fetch data for different tags.请指导我使用用于获取不同标签数据的通用语法。

Thanks.谢谢。

<dic class='test' data-attr='abc' ></div>
$('div.test').attr('data-attr')

you can check like this.你可以这样检查。

 $('*').filter(function() { if($(this).data('custom')){ console.log($(this).data('custom')); } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div data-custom="lol"> 1 </div> <p data-custom="wew"> 2 </p> <h1 data-custom="h1">h1</h1>

If you have different tags you can add a class to the tags, then select those elements by class name.如果您有不同的标签,您可以向标签添加一个类,然后按类名选择这些元素。

$('.my-class').each(function(){
    $(this).data('key', myVal);
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM