附加井号时未选择div [英] div not being selected when pound sign is appended

查看:122
本文介绍了附加井号时未选择div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于调试目的,这是我尝试在Chrome控制台中键入的内容:

For debugging purposes, this is what I tried to type in Chrome Console:

$("#loading")
>  null

但是,如果我这样做,它将正确地检索div:

But if I do this, it correctly retrieves the div:

$("loading")
>  <div id="loading" align="center" style="display: none;">

我正在使用jquery-1.4.1.min.js.

I'm using jquery-1.4.1.min.js.

<script type="text/javascript" src="../../js/jquery-1.4.1.min.js"></script>

这对我来说没有意义,为什么我不能用#号选择div,但排除时可以呢?

This doesn't make sense to me, why can I not select a div by # sign but I can when I exclude it?

对不起,我的失败很大.我的意思是相反的.请参阅修改后的问题.

Sorry, huge fail on my part. I meant the other way around. Please see the revised question.

我唯一拥有的其他js库是prototype.js,它是在jquery脚本之后加载的.

The only other js library i have is prototype.js, which is loaded after jquery script.

推荐答案

您应该看看 jQuery选择器文档.

遵循一些使用jQuery选择器的基本规则(这些绝不是详尽无遗的,您应该看一下文档):

Some basic rules when using jQuery selectors follow (these are by no means exhaustive, you should look at the docs):

  • 在选择器的开头使用#将会搜索所有id后面带有id的单词的所有DOM节点.因此,$('#loading')将选择id="loading"的DOM节点.这应该只返回一个元素,因为页面上的非唯一id是无效的HTML.
  • 在选择器的开头使用.会进行与#相似的搜索,但是会查看所有DOM节点的class属性,并选择与选择器匹配的类.因此,('.loading')将选择在class属性值中具有loading的DOM节点.
  • 仅使用没有前面符号的单词将尝试选择其元素标签名称与选择器的单词匹配的所有DOM节点.因此$('loading')会尝试查找所有<loading>标签,但是由于这不是实际的HTML标签,因此不会选择任何内容.
  • Using a # at the beginning of your selector will search for all DOM nodes with an id of whatever word follows the #. So $('#loading') will select DOM nodes with id="loading". This should only return one element, since non-unique ids on a page are invalid HTML.
  • Using a . at the beginning of your selector will do a similar search to #, but will look at all DOM nodes' class attributes instead and select those with a class matching your selector. So ('.loading') will select DOM nodes with loading in their class attribute's value.
  • Using simply a word with no preceding symbols will attempt to select all DOM nodes whose element tag name matches your selector's word. So $('loading') will attempt to find all <loading> tags, but since this isn't an actual HTML tag, nothing will be selected.

编辑 因此,尽管上述情况是正确的,但似乎您在prototype.js和jQuery之间存在冲突.这些都是众所周知的,令人感叹.您可以查看jQuery的与其他库一起使用jQuery的Wiki条目

EDIT So while the above is true, it seems that you had conflicts between prototype.js and jQuery. These are well known and much lamented. You can look at jQuery's wiki entry on using jQuery with other libraries and the documentation on jQuery.noConflict() for more information on this. Essentially, you will need to use jQuery instead of $ to access the jQuery library.

这篇关于附加井号时未选择div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆