简体   繁体   English

jQuery函数.click()适用于一个ID,但不适用

[英].click() jQuery function works for one ID but not variable

I have this code in my js file and basically what I want to test out is, if the user clicks on a certain button (which has ID of admin). 我的js文件中有这段代码,基本上,我要测试的是用户是否单击某个按钮(ID为admin)。

I want it to bring up a closeButton which is png image and then when the user clicks this again it should disappear. 我希望它调出一个为png图像的closeButton ,然后当用户再次单击时它应该消失。 To test of the button functions are responsive I have put alerts in the functions. 为了测试按钮功能是否响应,我在功能中添加了警报。

Clicking on the initial button works, the function finds the corresponding ID, makes the alert("jQuery Worked") line and brings up the closeButton image. 单击初始按钮有效,该函数将找到相应的ID,制作alert("jQuery Worked")行并显示closeButton图像。

However when I click on the close button nothing happens (we expect here that the alert("hiii") would work but it doesn't. I have looked online and found that my code needed to be in a $(document).ready(function() {} function which it is but it isn't working. I also tried to use the ID of the image to make the closeButton image disappear but that didn't work either. So I have tried just using the $closeButton variable which I thought for usre should work but doesn't. Why? 但是,当我单击关闭按钮时,什么也没发生(我们希望在这里alert("hiii")可以正常工作,但是没有。我在网上查看并发现我的代码需要放在$(document).ready(function() {}函数,但它不起作用。我还尝试使用图像的ID来使closeButton图像消失,但那也不起作用。因此,我尝试仅使用$closeButton我认为应该使用的变量应该起作用,但是不起作用,为什么?

.js file .js文件

var $closeButton = $("<img>");
$(document).ready(function() {
    $("#admin").click(function (event) {
        var $overlay = $("<div id='overlay'> </div>");
        var $closeButton = $("<img class='classMe' id='closeButtonID' src='https://s23.postimg.org/ouup1ib6z/close_button.png'></img>");
        $("body").append($overlay);
        $overlay.append($closeButton);  

        alert("jQuery worked");
    });

    $closeButton.click(function() {
        alert("hiiii");
    });
});

you looking for Event delegation. 您在寻找事件委托。

Event delegation refers to the process of using event propagation (bubbling) to handle events at a higher level in the DOM than the element on which the event originated. 事件委托是指使用事件传播(冒泡)在DOM中比事件起源的元素更高的级别处理事件的过程。 It allows us to attach a single event listener for elements that exist now or in the future. 它允许我们为当前或将来存在的元素附加单个事件侦听器。 Inside the Event Handling Function. 内部事件处理功能。

$(document).on('click', '#closeButtonID', function() {
     alert('hiii');
});

Do this: 做这个:

$(document).ready(function() {

    $("#admin").click(function (event) {
      var $overlay = $("<div id='overlay'></div>");
      var $closeButton = $("<img id='closeButtonID' src='https://s23.postimg.org/ouup1ib6z/close_button.png'></img>");
      $("body").append($overlay);
      $overlay.append($closeButton);  

      alert("jQuery worked");
    });

    $(document).on('click', '#closeButtonID', function() {
      alert('hi');
    });
});

Not a good way to do this but it's a different way to solve the problem , Hope it will help you : 这样做不是一个好方法,但是解决问题的方法却不同,希望它能对您有所帮助:

var $closeButton = $("<img>");
$(document).ready(function() {
    $("#admin").click(function (event) {
        var $overlay = $("<div id='overlay'> </div>");
        var $closeButton = $("<img class='classMe' id='closeButtonID' src='https://s23.postimg.org/ouup1ib6z/close_button.png'></img>");
        $("body").append($overlay);
        $overlay.append($closeButton);  

        alert("jQuery worked");
   close();
    });
  function close(){
    $('#closeButtonID').click(function() {
        alert("hiiii");
    });
}
});

JSFIDDLE JSFIDDLE

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

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