简体   繁体   English

$('div'); 返回一个空数组

[英]$('div'); returns an empty array

I'm trying to get all the div classes in a page using jQuery $('div');我正在尝试使用 jQuery $('div');获取页面中的所有 div 类$('div'); , it's on a page that doesn't support jquery but I load it using the links below until I get $ === jQuery is true. ,它在一个不支持 jquery 的页面上,但我使用下面的链接加载它,直到我得到$ === jQuery is true。

Even here on SO which has jQuery, when I do var x = $('post-text');即使在具有 jQuery 的 SO 上,当我执行var x = $('post-text'); x is empty. x为空。 What might be the reason for this not working?这可能是什么原因不起作用?

I do this in Chrome's console.我在 Chrome 的控制台中执行此操作。

How to run jQuery directly on any page in the browser? 如何直接在浏览器中的任何页面上运行 jQuery?

testing jQuery statements in Chrome JavaScript console 在 Chrome JavaScript 控制台中测试 jQuery 语句

You must use '.'您必须使用“.” before the class name and '#' before the id name of element as $('.classname') for accessing any html element在类名之前和元素的 id 名之前的 '#' 作为 $('.classname') 用于访问任何 html 元素

You can add jquery by adding it's online link in your head tag and with without adding jquery in your page you can't use it's features您可以通过在 head 标签中添加 jquery 的在线链接来添加 jquery,如果不在页面中添加 jquery,您将无法使用它的功能

var x = $('post-text'); var x = $('post-text'); seems wrong好像错了

append a .附加一个 . or # for class or ID或 # 用于类或 ID

var x = $('.post-text'); var x = $('.post-text'); OR var x = $('#post-text');或 var x = $('#post-text');

Sounds like jQuery might not be included on the page.听起来像 jQuery 可能不包含在页面中。 I have fallen victim to this before - Chrome console has a built-in selector engine also defined as $, meaning that when jQuery is not loaded on a page, $(...) can be used to do a DOM lookup.我以前也受过这种情况——Chrome 控制台有一个内置的选择器引擎,也定义为 $,这意味着当页面上未加载 jQuery 时,$(...) 可用于执行 DOM 查找。

Use $.fn.jquery in order to find out the version of jQuery loaded.使用 $.fn.jquery 找出加载的 jQuery 版本。 It should return something like "1.7.1" but the version you have.它应该返回诸如“1.7.1”之类的内容,但是您拥有的版本。 If it isn't defined, jQuery isn't loaded.如果未定义,则不会加载 jQuery。

first you add jquery file in page like this首先,您在页面中添加 jquery 文件,如下所示

var jquery = document.createElement('script'); 
jquery.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.head.appendChild(jquery);

now you can use all jquery functions现在你可以使用所有的 jquery 函数了

alert($('div').length);

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

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