JQuery,为标签设置属性

时间:2021-08-17 08:44:58

i am use $(expr).attr("hash",value) to set a "hash" attribute for HTML anchor element "a" but the Jquery would not do that. but if i change expr to "div", then i can set "hash" attribute to "div" tag.

我使用$(expr).attr(“hash”,value)为HTML锚元素“a”设置“哈希”属性,但Jquery不会这样做。但如果我将expr更改为“div”,那么我可以将“hash”属性设置为“div”标签。

is this behavior a xhtml specification? i can set "id" of "a" tag attribute. since id is a built in attribute for html "a" tag.

这个行为是xhtml规范吗?我可以设置“a”标签属性的“id”。因为id是html“a”标签的内置属性。

3 个解决方案

#1


to set a "hash" attribute for HTML anchor element "a"

为HTML锚元素“a”设置“哈希”属性

The <a> element (HTMLLinkElement) already has a DOM Level 0 hash property. It is used like window.location.hash to read or set the ‘...#anchor’ part at the end of the URL being referred to by the element's href.

元素(HTMLLinkElement)已经具有DOM Level 0哈希属性。它像window.location.hash一样用于读取或设置元素的href引用的URL末尾的“...#anchor”部分。

Setting a.hash, whether directly or through jQuery's attr() wrapper, merely sets the anchor name in the link's URL. You could deliberately say you want an actual attribute by calling the DOM method a.setAttribute('hash', value), except that this doesn't work in IE6/7 due to a long-standing bug where it confuses attributes and properties.

无论是直接还是通过jQuery的attr()包装器设置a.hash,只需在链接的URL中设置锚名称即可。您可以通过调用DOM方法a.setAttribute('hash',value)来故意说您想要一个实际的属性,除了这在IE6 / 7中不起作用,因为它会混淆属性和属性的长期错误。

This is one of the problems with adding custom non-standard attributes to elements, you never know when it's going to * with an existing name. HTML5 will suggest you limit your custom attributes to names starting with ‘data-’, but in general it's best to find another way of storing data if you can.

这是向元素添加自定义非标准属性的问题之一,您永远不知道它何时会与现有名称冲突。 HTML5会建议您将自定义属性限制为以“data-”开头的名称,但一般来说,如果可以的话,最好找到另一种存储数据的方法。

#2


Probably your trouble is that there isn't any hash attribute for an <a> tag. Perhaps you're looking for the name attribute, or maybe you want to change the hash in the link href, in which case you'll have to parse the link text and replace the hash using regular expressions.

可能你的麻烦是标签没有任何哈希属性。也许您正在寻找name属性,或者您想要更改链接href中的哈希值,在这种情况下,您将不得不解析链接文本并使用正则表达式替换哈希值。

#3


Another option for setting the hash. this only works where the expression returns an a element. This is because hash is a property on the actual a dom element.

设置哈希的另一个选项。这仅适用于表达式返回元素的位置。这是因为hash是实际dom元素的属性。

$(expr).each(function() {
  this.hash = value;
});

if your need to test to see whether its an a tag use this

如果你需要测试,看看它是一个标签使用这个

$(expr).is('a').each(function() {
  this.hash = value;
});

in the case where you actually want to append some custom attribute I would recommend adding the value using the data method

在您实际想要附加一些自定义属性的情况下,我建议使用数据方法添加值

$(expr).data('myHash', value);

#1


to set a "hash" attribute for HTML anchor element "a"

为HTML锚元素“a”设置“哈希”属性

The <a> element (HTMLLinkElement) already has a DOM Level 0 hash property. It is used like window.location.hash to read or set the ‘...#anchor’ part at the end of the URL being referred to by the element's href.

元素(HTMLLinkElement)已经具有DOM Level 0哈希属性。它像window.location.hash一样用于读取或设置元素的href引用的URL末尾的“...#anchor”部分。

Setting a.hash, whether directly or through jQuery's attr() wrapper, merely sets the anchor name in the link's URL. You could deliberately say you want an actual attribute by calling the DOM method a.setAttribute('hash', value), except that this doesn't work in IE6/7 due to a long-standing bug where it confuses attributes and properties.

无论是直接还是通过jQuery的attr()包装器设置a.hash,只需在链接的URL中设置锚名称即可。您可以通过调用DOM方法a.setAttribute('hash',value)来故意说您想要一个实际的属性,除了这在IE6 / 7中不起作用,因为它会混淆属性和属性的长期错误。

This is one of the problems with adding custom non-standard attributes to elements, you never know when it's going to * with an existing name. HTML5 will suggest you limit your custom attributes to names starting with ‘data-’, but in general it's best to find another way of storing data if you can.

这是向元素添加自定义非标准属性的问题之一,您永远不知道它何时会与现有名称冲突。 HTML5会建议您将自定义属性限制为以“data-”开头的名称,但一般来说,如果可以的话,最好找到另一种存储数据的方法。

#2


Probably your trouble is that there isn't any hash attribute for an <a> tag. Perhaps you're looking for the name attribute, or maybe you want to change the hash in the link href, in which case you'll have to parse the link text and replace the hash using regular expressions.

可能你的麻烦是标签没有任何哈希属性。也许您正在寻找name属性,或者您想要更改链接href中的哈希值,在这种情况下,您将不得不解析链接文本并使用正则表达式替换哈希值。

#3


Another option for setting the hash. this only works where the expression returns an a element. This is because hash is a property on the actual a dom element.

设置哈希的另一个选项。这仅适用于表达式返回元素的位置。这是因为hash是实际dom元素的属性。

$(expr).each(function() {
  this.hash = value;
});

if your need to test to see whether its an a tag use this

如果你需要测试,看看它是一个标签使用这个

$(expr).is('a').each(function() {
  this.hash = value;
});

in the case where you actually want to append some custom attribute I would recommend adding the value using the data method

在您实际想要附加一些自定义属性的情况下,我建议使用数据方法添加值

$(expr).data('myHash', value);