Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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 未捕获类型错误:无法读取属性';增补列表器';空值(查询选择器)_Javascript_Event Listener - Fatal编程技术网

Javascript 未捕获类型错误:无法读取属性';增补列表器';空值(查询选择器)

Javascript 未捕获类型错误:无法读取属性';增补列表器';空值(查询选择器),javascript,event-listener,Javascript,Event Listener,我得到以下错误: 未捕获的TypeError:无法读取null的属性“addEventListener” addEventListener在单个id(菜单)上运行良好。。。有没有限制我不能在querySelector上使用它 (是的,JavaScript位于HTML文档的底部) 任何帮助都将不胜感激 尝试更改: var area = document.querySelector("#menu + #envelope + #links"); 用于: 由于您的html是: <div id=

我得到以下错误:

未捕获的TypeError:无法读取null的属性“addEventListener”

addEventListener
在单个id(菜单)上运行良好。。。有没有限制我不能在querySelector上使用它

(是的,JavaScript位于HTML文档的底部)

任何帮助都将不胜感激

尝试更改:

var area = document.querySelector("#menu + #envelope + #links");
用于:

由于您的html是:

<div id="menu">Click here to browse the internet.
   <div id="envelope">
      <div id="links" >
         ...
      </div>
   </div>
</div>
因为如果文档验证,ID必须是唯一的。

尝试更改:

var area = document.querySelector("#menu + #envelope + #links");
用于:

由于您的html是:

<div id="menu">Click here to browse the internet.
   <div id="envelope">
      <div id="links" >
         ...
      </div>
   </div>
</div>

因为如果文档验证,ID必须是唯一的。

在window.onload()中包装Javascript


看起来浏览器在“执行HTML”/“生成页面”之前正在执行JavaScript,因此这些HTML元素/DOM节点还不存在。这会引入一个延迟,直到加载HTML,这样在JavaScript尝试将方法附加到节点之前,节点就已经存在(不是null)。(还有下面jprubio对选择器的介绍。)

在window.onload()中包装Javascript


看起来浏览器在“执行HTML”/“生成页面”之前正在执行JavaScript,因此这些HTML元素/DOM节点还不存在。这会引入一个延迟,直到加载HTML,这样在JavaScript尝试将方法附加到节点之前,节点就已经存在(不是null)。(还有下面jprubio对您的选择器所说的。)

我已经编辑了您的plunk,以适应您犯的两个小错误

第一个错误是将脚本包含在标记中。 我在结束语前加了一个

此外,正如@jeprubio所提到的,您需要从querySelector()调用中删除“+”

这是经过编辑的plunk

代码

//代码在这里

var menu = document.getElementById("menu");
var area = document.querySelector("#menu #envelope #links");

menu.addEventListener("mouseenter", addHref);
area.addEventListener("mouseleave", remHref);

menu.addEventListener("click", addHref);
document.addEventListener("click", function (){
    if (this != area){
        remHref();
    } 
});

function remHref (){
    document.getElementById("google").removeAttribute("href");
    document.getElementById("mysite").removeAttribute("href");
}

function addHref (){
    setTimeout(activate, 2500);
}

function activate (){
    document.getElementById("google").setAttribute("href", "https://www.google.com");
    document.getElementById("mysite").setAttribute("href", "https://www.mywebsite.com");
}

我编辑了你的日志,以适应你犯的两个小错误

第一个错误是将脚本包含在标记中。 我在结束语前加了一个

此外,正如@jeprubio所提到的,您需要从querySelector()调用中删除“+”

这是经过编辑的plunk

代码

//代码在这里

var menu = document.getElementById("menu");
var area = document.querySelector("#menu #envelope #links");

menu.addEventListener("mouseenter", addHref);
area.addEventListener("mouseleave", remHref);

menu.addEventListener("click", addHref);
document.addEventListener("click", function (){
    if (this != area){
        remHref();
    } 
});

function remHref (){
    document.getElementById("google").removeAttribute("href");
    document.getElementById("mysite").removeAttribute("href");
}

function addHref (){
    setTimeout(activate, 2500);
}

function activate (){
    document.getElementById("google").setAttribute("href", "https://www.google.com");
    document.getElementById("mysite").setAttribute("href", "https://www.mywebsite.com");
}

您的代码很好您只需:

  • 在关闭标记之前放置脚本

  • 将元素区域的查询选择器从
    “#menu+#envelope+#links”
    更正为
    “#menu#envelope#links”

例如:

//代码在这里
var menu=document.getElementById(“菜单”);
var区域=document.querySelector(“#菜单#信封#链接”);
menu.addEventListener(“mouseenter”,addHref);
area.addEventListener(“mouseleave”,remHref);
menu.addEventListener(“单击”,addHref);
document.addEventListener(“单击”),函数(){
如果(此!=区域){
remHref();
}
});
函数remHref(){
document.getElementById(“谷歌”).removeAttribute(“href”);
document.getElementById(“mysite”).removeAttribute(“href”);
}
函数addHref(){
设置超时(激活,2500);
}
功能激活(){
document.getElementById(“谷歌”).setAttribute(“href”,”https://www.google.com");
document.getElementById(“mysite”).setAttribute(“href”,”https://www.mywebsite.com");
}
/*样式在这里*/
#菜单{
高度:10vh;
背景色:红色;
文本对齐:居中;
过渡:所有1都放松;
垫顶:5vh;
}
#菜单:悬停{
颜色:红色;
}
#封套{
身高:0;
显示:块;
可见性:隐藏;
背景颜色:蓝色;
最小宽度:100%;
z指数:1;
位置:绝对位置;
左:0;
内容:“;
不透明度:0;
过渡:所有1.3秒放松;
}
#链接{
身高:0;
可见性:隐藏;
显示器:flex;
背景颜色:粉红色;
证明内容:中心;
z指数:2;
最小宽度:100%;
不透明度:0;
过渡:所有1都容易进入;
}
#谷歌{
利润上限:-1vh;
宽度:150px;
}
#麦斯特{
左:5%;
利润上限:-1vh;
宽度:150px;
}
#菜单:悬停#信封{
高度:100px;
不透明度:1;
能见度:可见;
}
#菜单:焦点#信封{
高度:100px;
不透明度:1;
能见度:可见;
}
#菜单:悬停链接{
不透明度:1;
高度:300px;
能见度:可见;
}
#菜单:焦点链接{
不透明度:1;
高度:300px;
能见度:可见;
}

单击此处浏览internet。1

您的代码很好您只需:

  • 在关闭标记之前放置脚本

  • 将元素区域的查询选择器从
    “#menu+#envelope+#links”
    更正为
    “#menu#envelope#links”

例如:

//代码在这里
var menu=document.getElementById(“菜单”);
var区域=document.querySelector(“#菜单#信封#链接”);
menu.addEventListener(“mouseenter”,addHref);
area.addEventListener(“mouseleave”,remHref);
menu.addEventListener(“单击”,addHref);
document.addEventListener(“单击”),函数(){
如果(此!=区域){
remHref();
}
});
函数remHref(){
document.getElementById(“谷歌”).removeAttribute(“href”);
document.getElementById(“mysite”).removeAttribute(“href”);
}
函数addHref(){
设置超时(激活,2500);
}
功能激活(){
document.getElementById(“谷歌”).setAttribute(“href”,”https://www.google.com");
document.getElementById(“mysite”).setAttribute(“href”,”https://www.mywebsite.com");
}
/*样式在这里*/
#菜单{
高度:10vh;
背景色:红色;
文本对齐:居中;
过渡:所有1都放松;
垫顶:5vh;
}
#菜单:悬停{
颜色:红色;
}
#封套{
身高:0;
显示:块;
可见性:隐藏;
背景颜色:蓝色;
最小宽度:100%;
z指数:1;
位置:绝对位置;
左:0;
内容:“;
不透明度:0;
过渡:所有1.3秒放松;
}
#链接{
身高:0;
可见性:隐藏;
显示器:flex;
背景颜色:粉红色;
证明内容:中心;
z指数:2;
最小宽度:100%;
不透明度:0;
反式
var area = document.querySelector("#links");
window.onload=function() {
   /*your code here*
   /*var date = document.getElementById("date");
   /*alert(date);
}
var menu = document.getElementById("menu");
var area = document.querySelector("#menu #envelope #links");

menu.addEventListener("mouseenter", addHref);
area.addEventListener("mouseleave", remHref);

menu.addEventListener("click", addHref);
document.addEventListener("click", function (){
    if (this != area){
        remHref();
    } 
});

function remHref (){
    document.getElementById("google").removeAttribute("href");
    document.getElementById("mysite").removeAttribute("href");
}

function addHref (){
    setTimeout(activate, 2500);
}

function activate (){
    document.getElementById("google").setAttribute("href", "https://www.google.com");
    document.getElementById("mysite").setAttribute("href", "https://www.mywebsite.com");
}