Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用自定义属性有效吗?_Jquery_Html - Fatal编程技术网

Jquery 使用自定义属性有效吗?

Jquery 使用自定义属性有效吗?,jquery,html,Jquery,Html,我想取消任何链接并为每个链接添加额外属性。下面是我如何做到这一点的 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

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

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');
    });
}

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

有效如果您使用HTML5,您可以添加自己的自定义数据属性
数据-


您可以使用jQuery来读取和修改它们。

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

和HTML:

<a href="somewhere.html" data-hrefnot="something">A link</a>

如果您最初不需要在标记上存储数据,那么无论您是否使用HTML5 doctype(jQuery的
data
方法仍然有效)。

不,您应该将其用于自定义元素属性,或者(或者更好地)用于标准html属性


data()用法示例:


但如果我这样做,我就看不到firebut中的当前值。正常吗?是的,正常。jQuery在内部存储
数据
(这样,如果没有使用HTML5 doctype,您仍然可以使用有效的标记)。
<a href="somewhere.html" data-hrefnot="something">A link</a>
<a hred="some/url" data-custom="value">test</a>

// getter
var customValue = $('a').data('custom');
var customUndefined = $('a').data('testing');
// setter
$('a').data('custom', 'test');
$('a').data('testing', true);