JQuery ,为 <a> 标签设置属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/535140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:17:28  来源:igfitidea点击:

JQuery , set attribute for <a> tag

jqueryattributes

提问by bobince

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”设置“hash”属性,但 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" 标签的内置属性。

回答by bobince

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

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

The <a> element (HTMLLinkElement) already has a DOM Level 0 hashproperty. 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.

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

Setting a.hash, whether directly or through jQuery's attr()wrapper, merely sets the anchor name in the link's URL. You coulddeliberately 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.

Setting a.hash,无论是直接设置还是通过 jQuery 的attr()包装器,都只是在链接的 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 clash 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-”开头的名称,但总的来说,如果可以,最好找到另一种存储数据的方式。

回答by Sophie Alpert

Probably your trouble is that there isn't any hashattribute for an <a>tag. Perhaps you're looking for the nameattribute, 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.

可能您的问题是标签没有任何hash属性<a>。也许您正在寻找name属性,或者您想更改链接中的哈希值href,在这种情况下,您必须解析链接文本并使用正则表达式替换哈希值。

回答by bendewey

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.

设置散列的另一个选项。这仅适用于表达式返回 a 元素的情况。这是因为 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

如果您确实想要附加一些自定义属性,我建议您使用 data 方法添加值

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