使用自定义属性有效吗?

Ton*_*bet 4 html jquery

我想取消任何链接并为每个链接添加额外的属性.以下是我如何实现这一目标.

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');
    });
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,这是一种有效/推荐的添加自定义属性的方法吗?

Jam*_*ice 7

不是这样的.如果您使用的是HTML5 doctype,则可以使用新data-*属性,然后使用jQuery data方法:

$(this).data("hrefnot", href);
Run Code Online (Sandbox Code Playgroud)

和HTML:

<a href="somewhere.html" data-hrefnot="something">A link</a>
Run Code Online (Sandbox Code Playgroud)

如果您最初不需要在标签上存储数据,那么您是否使用HTML5 doctype并不重要(jQuery的data方法仍然有用).