是否使用自定义属性有效?

编程入门 行业动态 更新时间:2024-10-20 16:41:38
本文介绍了是否使用自定义属性有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想取消任何链接并为每个链接添加额外的属性。下面是我如何实现这一点。

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).

更多推荐

是否使用自定义属性有效?

本文发布于:2023-10-27 03:12:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!