简体   繁体   English

JavaScript警报回调在函数内部不起作用

[英]JavaScript alert callback not working inside a function

I am trying to make a image preview containing of about 5-6 images which will appear one after another when user hovers over it (not like a carousel with prev and next buttons). 我正在尝试制作一个包含约5-6张图像的图像预览,当用户将鼠标悬停在图像上时,它们会陆续出现(不像带有上一个和下一个按钮的轮播)。 Here is the fiddle consisting of what I gathered so far.. i don't know if this approach is right or not.. but I am stuck as the alert callback is not working. 这是我到目前为止收集的小提琴 。.我不知道这种方法是否正确..但由于警报回调不起作用,我被卡住了。 Could someone please tell me what is wrong? 有人可以告诉我怎么了吗?

$(function() 
  {
      var imageCount = $('#product_grid_list').find('figure')[0].getElementsByTagName('img');

      for (var i = 0, n = imageCount.length; i < n; i++) {
          imageCount[i].on('click', function(e) 
              {
                  alert('Everything is going fine!');
              }          
          );
      }
  }
);

You are attempting to call on() , a jQuery method, on an HTMLElement (a DOM element). 您正试图调用on()一个jQuery方法,在一个HTMLElement (DOM元素)。 You can't do that, jQuery methods can only be called on jQuery collections. 您不能这样做,只能在jQuery集合上调用jQuery方法。 It's easy to get a jQuery collection for the elements you desire: 可以轻松获取所需元素的jQuery集合:

  • Use .find() to match the images 使用.find()匹配图像
  • There's no need for a for() loop, jQuery's .on() will handle looping for you. 不需要for for()循环,jQuery的.on()将为您处理循环。
  • You may also want to prevent the default behaviour of your anchors 您可能还想防止锚的默认行为

$(function () {
    var imageCount = $('#product_grid_list').find('figure img');
    imageCount.on('click', function (e) {
        e.preventDefault()
        alert('Everything is going fine!');
    })
});

JSFiddle JSFiddle

You are using js AND jQuery at same time. 您正在同时使用js和jQuery。 It's wrong. 这是不对的。 If you use jQuery, than click event will be like this: 如果使用jQuery,则click事件将如下所示:

$(document).('click', '#product_grid_list figure img', function(){
     alert('Everything is going fine!');
});

You are using a mix of jQuery and standalone javascript. 您正在混合使用jQuery和独立javascript。 You might as well go all the way to jQuery, with something like: 您不妨一路使用jQuery,例如:

$('#product_grid_list figure:first img').click(function(e) {
      alert('Everything is going fine, hopefully!');
   });

You did not send the corresponding HTML, so we cannot test whether the above is correct. 您没有发送相应的HTML,因此我们无法测试以上内容是否正确。

it's just a simple click event in jQuery, no need to use js: http://jsfiddle.net/wP3QQ/11/ 这只是jQuery中的简单点击事件,无需使用js: http : //jsfiddle.net/wP3QQ/11/

$('#product_grid_list').find('figure img').click(function(e){
    alert('Everything is going fine!');
    e.preventDefault();
    });

You want the hover effect, so click event should not be used over here. 您需要悬停效果,因此不应在此处使用click事件。 It should be mouseover. 应该是鼠标悬停。

Working Fiddle 工作小提琴

Code Snippet: 代码段:

$(document).on('mouseover','#product_grid_list  figure  img',function(e){
      alert("now it is working");
});

The root cause of click event callback can't be triggered is that you're trying to register a event handler on a "DOM" (in this case: imageCount[i] ) element in jQuery way. 无法触发点击事件回调的根本原因是,您试图以jQuery方式在“ DOM”(在本例中为imageCount [i] )元素上注册事件处理程序。 Try to register the event handler like this if you want to use pure javascript solution: 如果您想使用纯JavaScript解决方案,请尝试像这样注册事件处理程序:

imageCount[i].addEventListener('click', function(e){
    alert('Everything is going fine!');
});

Here is a jsfiddle demo . 这是一个jsfiddle演示

Note: I didn't consider the cross browser issue in this case. 注意:在这种情况下,我没有考虑跨浏览器的问题。

BTW, try to cache the length of imageCount node list, it will improve the performance. 顺便说一句,尝试缓存imageCount节点列表的长度,它将提高性能。

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

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