0

鉴于此 HTML:

<p htmlfor="afield">This field is required.</p>

这个 jQuery 选择器会找到 p 标签:

var filtered = $("*").filter("[for='afield']");

而这个不会:

$("p").filter("[for='afield']");

为什么第一个匹配?HTML 中的属性是“htmlfor”,而选择器中的属性是“for”。

而且,为什么第二个匹配?

这是一个小提琴(带有警报,抱歉......)。

http://jsfiddle.net/f7v4h/1/

从评论中编辑解释我为什么关心:

我正在使用 jquery-validate 插件。[for='afield']是插件代码使用的语法,它也是生成 HTML 的插件。我只是想了解发生了什么,以便修复我网站上的错误。

4

1 回答 1

1

Upgrade jQuery validate to 1.9.0 to see if it resolves the concern. You can live-link it from Microsoft's CDN: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js

EDIT:

It seems that upgrading jQuery and jQuery.validate resolved the "it isn't working" but not the "why". Attempting to answer the "why":

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-213157251 seems to suggest that these properties are exceptions and are thus renamed in awkward ways. Perhaps this is another great example of jQuery abstracting away annoying browser abnormalities so we can just get the job done.

于 2012-05-22T00:25:43.410 回答