本文介绍了是否使用自定义属性有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想取消任何链接并为每个链接添加额外的属性。下面是我如何实现这一点。
I want to cancel any links and add extra attributes to each one. Below is how I've achieved this.
function anularEnlaces(){ $('nav a').each(function(){ var href = $(this).attr('href'); var id = $(this).attr('id'); $(this).attr('href','javascript:void(0)'); $(this).attr('hrefnot',href); $(this).attr('nivel','uno'); $(this).attr('seccion','dos'); }); }我的问题是,这是一种有效/推荐的方式来添加自定义的属性?
My question is, is that a valid/recommended way to add custom atributes?
推荐答案不是那样的。如果您使用HTML5文档类型,则可以使用新的 data - * 属性,然后使用jQuery data 方法:
Not like that. If you are using the HTML5 doctype you can use the new data-* attributes, and then use the jQuery data method:
$(this).data("hrefnot", href);和HTML:
And HTML:
<a href="somewhere.html" data-hrefnot="something">A link</a>如果您最初不需要在您的标签上存储数据,那么无论是否你使用HTML5的文档类型(jQuery的 data 方法仍然有效)。
If you don't need to store data on your tags initially, then it doesn't matter whether you use the HTML5 doctype or not (jQuery's data method will still work).
更多推荐
是否使用自定义属性有效?
发布评论