Javascript 类型错误:$(...).children 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28996811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: $(...).children is not a function
提问by Hello lad
I try to select certain DOM element with jQuery
我尝试使用 jQuery 选择某些 DOM 元素
the html
html
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>
</head>
<body>
<nav id="nav" class="navigator">
<h1>Nav Header</h1>
<ul class="nav-list">
<li class="nav-item"><a >Item #1</a></li>
<li class="nav-item active"><a href="#2">Item #2</a></li>
</ul>
</nav>
</body>
</html>
I want to select Item #1 I used
我想选择我用过的 Item #1
$('.nav-list').children()
I got
我有
TypeError: $(...).children is not a function
What's wrong here ?
这里有什么问题?
回答by JLRishe
You need to include jQuery in your page.
您需要在页面中包含 jQuery。
Most browsers nowadays include a $()function in their console by default for easy element selection, but this simply maps to document.getElementById().
现在大多数浏览器都$()默认在其控制台中包含一个函数,以便轻松选择元素,但这只是映射到document.getElementById().
The value returned will not have a .children()method.
返回的值将没有.children()方法。
Additionally, if you load an HTML page directly from your file system, you need to include the http://for CDN script URLs. Otherwise, your browser will try to find the .js file on your local system.
此外,如果直接从文件系统加载 HTML 页面,则需要包含http://for CDN 脚本 URL。否则,您的浏览器将尝试在您的本地系统上查找 .js 文件。
回答by olik79
Going from the fact that $ doesn't refer to jQuery in the browser's console I found a way to change this for the current session within Google Chrome. I just pasted the JavaScript source from this github page into the console:
从 $ 不在浏览器控制台中引用 jQuery 的事实出发,我找到了一种在 Google Chrome 中为当前会话更改此设置的方法。我刚刚将此 github 页面中的 JavaScript 源代码粘贴到控制台中:
use jQuery in Chrome javascript console
在 Chrome javascript 控制台中使用 jQuery
The code for future reference:
供以后参考的代码:
var jq = document.createElement('script');
jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jQuery.noConflict();
After that I could
之后我可以
$("html").children()

