Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:$(…)。子项不是函数_Javascript_Jquery - Fatal编程技术网

Javascript TypeError:$(…)。子项不是函数

Javascript TypeError:$(…)。子项不是函数,javascript,jquery,Javascript,Jquery,我尝试使用jQuery选择某些DOM元素 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"> <

我尝试使用jQuery选择某些DOM元素

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>
我得到


这里怎么了?

您需要在页面中包含jQuery

现在的大多数浏览器在其控制台中默认包含一个
$()
函数,以便于选择元素,但这只是映射到
document.getElementById()

返回的值将没有
.children()
方法


此外,如果直接从文件系统加载HTML页面,则需要为CDN脚本URL包含
http://
。否则,您的浏览器将尝试在您的本地系统上查找.js文件。

由于$s在浏览器控制台中没有引用jQuery,我找到了一种方法,可以在Google Chrome中为当前会话更改此文件。我刚刚将此github页面中的JavaScript源粘贴到控制台中:

该守则供日后参考:

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();
在那之后我可以

$("html").children()

你忘了包含jQuery了吗?@scimonester我在Chrome的JS控制台中尝试过Chrome JS控制台默认包含一个
$()
函数,但它不是jQuery。你需要包含jQuery@Hellold:jQuery不包括在控制台中
$
被定义为
文档。默认情况下,getElementById
在那里。我确实包含了jQuery库,只是忘了发布它
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();
$("html").children()