简体   繁体   English

在jQuery选择器中使用hash

[英]Use of hash in jQuery selectors

I am using jQuery 1.8.0 as part of a WordPress plugin and storing mouse click x/y coordinates and keeping the node information including the parent path information (similar to Getting a jQuery selector for an element ). 我使用jQuery 1.8.0作为WordPress插件的一部分并存储鼠标单击x / y坐标并保持节点信息包括父路径信息(类似于获取元素的jQuery选择器 )。

After some experimentation I noticed that jQuery could not read some of the selectors I had stored. 经过一些实验,我注意到jQuery无法读取我存储的一些选择器。 Some examples: 一些例子:

html#:eq(0) -> the '#' is not necessary but jQuery evaluates it with or without the '#'! html#:eq(0) - >'#'不是必需的,但是jQuery使用或不使用'#'来评估它!
body#:eq(0) -> jQuery cannot evaluate this, it aborts/stops at this line (different behavior to html tag) body#:eq(0) - > jQuery无法对此进行评估,它会在此行中止/停止(对html标记的行为不同)

div#:eq(1) -> jQuery evaluates this (should abort?) div#:eq(1) - > jQuery评估这个(应该中止?)
div:eq(1) -> jQuery aborts/stops at this line (should work?) div:eq(1) - > jQuery在这一行中止/停止(应该工作吗?)

Is this a bug (if it is I will report it to jQuery)? 这是一个错误(如果我将它报告给jQuery)? My understanding from comments made to this question is that the '#' should be present if and only if there is an id for the element but this does not seem to be how jQuery is behaving. 我对这个问题的评论的理解是,当且仅当元素有id时才会出现'#',但这似乎不是jQuery的行为方式。

The hash is part of an id CSS selector , so it has to be included when you are targeting elements based on some id and it must not be included in all other situations. 哈希是id CSS选择器的一部分,因此当您基于某些ID定位元素时必须包含它,并且它不能包含在所有其他情况中。

All of your examples fall into the latter category. 您的所有示例都属于后一类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM