Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.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 jQuery在两个ID之间切换_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery在两个ID之间切换

Javascript jQuery在两个ID之间切换,javascript,jquery,html,Javascript,Jquery,Html,为什么下面的代码不起作用?红色方块变为绿色,但第二次单击不会再次变为红色 jQuerydocument.readyfunction${ $red.clickfunction{ $this.attrid,绿色; }; $green.clickfunction{ $this.attrid,红色; }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; } 问题是因为在绿色元素存在之前,在加载时分配了事件处理程序

为什么下面的代码不起作用?红色方块变为绿色,但第二次单击不会再次变为红色

jQuerydocument.readyfunction${ $red.clickfunction{ $this.attrid,绿色; }; $green.clickfunction{ $this.attrid,红色; }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; }
问题是因为在绿色元素存在之前,在加载时分配了事件处理程序

要解决此问题,您需要使用委派事件处理程序,以便在事件发生时对元素选择器进行评估:

jQueryfunction${ $document.on'click',红色,函数{ $this.attrid,绿色; }; $document.on'click',绿色,函数{ $this.attrid,红色; }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; }
问题是因为在绿色元素存在之前,在加载时分配了事件处理程序

要解决此问题,您需要使用委派事件处理程序,以便在事件发生时对元素选择器进行评估:

jQueryfunction${ $document.on'click',红色,函数{ $this.attrid,绿色; }; $document.on'click',绿色,函数{ $this.attrid,红色; }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; }
之所以会发生这种情况,是因为在加载页面时,没有id为绿色的元素可用于添加单击事件

相反,您可以通过以下方式使用事件委派:

$(document).on('click', '#red', function() {...}

这样,单击事件将应用于id为绿色的元素

请参见下面的工作示例:

jQuerydocument.readyfunction${ $document.on'click',red',函数{ $this.attrid,绿色; }; $document.on'click',green',函数{ $this.attrid,红色; }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; }
之所以会发生这种情况,是因为在加载页面时,没有id为绿色的元素可用于添加单击事件

相反,您可以通过以下方式使用事件委派:

$(document).on('click', '#red', function() {...}

这样,单击事件将应用于id为绿色的元素

请参见下面的工作示例:

jQuerydocument.readyfunction${ $document.on'click',red',函数{ $this.attrid,绿色; }; $document.on'click',green',函数{ $this.attrid,红色; }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; }
由于id是动态添加的,加载代码$green.clickfunction{时将找不到id为绿色的元素,因此不会为该元素设置click事件。因此,您可以在文档级别添加侦听器:

jQuerydocument.readyfunction${ $document.on'click',div',函数{ var id=$this.attr'id'; ifid==‘红色’{ $this.attrid,绿色; }否则{ $this.attrid,红色; } }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; }
由于id是动态添加的,加载代码$green.clickfunction{时将找不到id为绿色的元素,因此不会为该元素设置click事件。因此,您可以在文档级别添加侦听器:

jQuerydocument.readyfunction${ $document.on'click',div',函数{ var id=$this.attr'id'; ifid==‘红色’{ $this.attrid,绿色; }否则{ $this.attrid,红色; } }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; } 首先,有一个id为红色的div,单击该div后,它会动态地将id切换为绿色,所以在本例中我们使用它-

jQuerydocument.readyfunction${ $.parent。单击“红色”按钮,函数{ $this.attrid,绿色; }; $.parent。单击“绿色”按钮,函数{ $this.attrid,红色; }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; } 首先,有一个id为红色的div,单击该div后,它会动态地将id切换为绿色,所以在本例中我们使用它-

jQuerydocument.readyfunction${ $.parent。单击“红色”按钮,函数{ $this.attrid,绿色; }; $.parent。单击“绿色”按钮,函数{ $this.attrid,红色; }; }; div{ 保证金:0自动; 宽度:100px; 高度:100px; } 红色的{ 背景色:红色; } 绿色的{ 背景颜色:绿色; } 更改脚本

 $(document).on('click','#red',function() {
    $('#red').attr("id", "green");
  });

  $(document).on('click','#green',function() {
    $('#green').attr("id", "red");
  });
更改脚本

 $(document).on('click','#red',function() {
    $('#red').attr("id", "green");
  });

  $(document).on('click','#green',function() {
    $('#green').attr("id", "red");
  });

男人你真的很快:男人你真的很快:几乎完全依靠非现场内容来解释。从链接页面中摘录一小段关于为什么需要活动授权的内容可以改进这个问题的答案
委托允许我们将单个事件侦听器附加到父元素,该事件侦听器将为与选择器匹配的所有子体触发,无论这些子体现在存在还是将来添加。我已更新了答案。谢谢你指出我的答案@Lewist解释和深入信息的结合对我帮助很大!我的荣幸:@thomasmaleyrelies几乎只在非网站内容上解释。从链接页面中摘录一小段关于为什么需要事件委派的内容将改进此答案。事件委派允许我们将单个事件侦听器附加到父元素,该事件侦听器将为与选择器匹配的所有子体触发,无论这些子体现在存在还是将来添加。我已更新了答案。谢谢你指出我的答案@Lewist解释和深入信息的结合对我帮助很大!我的荣幸:@thomasmaley