jQuery $('。classname')。长度失败,但$ .find('。classname')。长度工作,为什么? [英] jQuery $('.classname').length fails, but $.find('.classname').length works, why?

查看:312
本文介绍了jQuery $('。classname')。长度失败,但$ .find('。classname')。长度工作,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使元素存在,我的JavaScript模块也无法使用 $('。classname')查找具有类名的特定DOM元素。
$ b $ $('。classname')。length 返回0,其中 $。 find('。classname')。length 返回1。



网站也包含其他JavaScript模块。

我的jQuery版本是1.7.1。

/ p>

重现此
1-转至avc.com

2-打开控制台。

3-粘贴控制台中的以下代码。

  var head = document.getElementsByTagName(head)[0]; 
var script = document.createElement(script);
script.type =text / javascript;
script.src =//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js;
script.async = true;
head.appendChild(script);

4-尝试

$('。pkg ').length $。find('。pkg')。length

从这里找到屏幕截图 http://i.imgur.com/4UDHz.png

还有一点,如果你在其他一些网站上试用这个功能,方案

您可能与其他JavaScript模块有一些冲突。看看jQuery noconflict


许多JavaScript库都使用$作为函数或变量名称,而jQuery所使用的只是
。在jQuery的情况下,$是jQuery的别名,所以
的所有功能都可以不使用$。如果我们需要使用
和jQuery一起使用另一个JavaScript库,我们可以通过调用$ .noConflict()来返回
$的控制权给另一个库。



尝试使用

  jQuery('。classname')。length 


My JavaScript module fails to find a particular DOM element with a classname using $('.classname') even when the element exists.

$('.classname').length returns 0, where as $.find('.classname').length returns 1.

Website contains other JavaScript modules also.

Can anyone help me to find why this is happening?

My jQuery version is 1.7.1.

To reproduce this 1- Go to avc.com
2- Open console.
3- Paste the following code in console.

var head = document.getElementsByTagName("head")[0];  
var script = document.createElement("script");  
script.type = "text/javascript";  
script.src = "//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";  
script.async = true;  
head.appendChild(script);

4- Try
$('.pkg').length and $.find('.pkg').length.

Find the screen-shot from here http://i.imgur.com/4UDHz.png

One more point, if you try this in some other websites, It works correctly.

解决方案

You may have some conflicts with your other JavaScript modules. Look into jQuery noconflict

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict()

Try using

jQuery('.classname').length

这篇关于jQuery $('。classname')。长度失败,但$ .find('。classname')。长度工作,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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