javascript jQuery event.target 在 Firefox 和 IE 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5846446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:33:31  来源:igfitidea点击:

jQuery event.target not working in firefox and IE?

javascriptjquery

提问by CaffeinatedCM

I'm working on making an image slider that loads the image the user clicks on using jQuery. I have it working great in Chrome but when I tried it in firefox and IE it's not loading the image at all. Here's my code:

我正在制作一个图像滑块,用于加载用户使用 jQuery 单击的图像。我在 Chrome 中运行良好,但是当我在 Firefox 和 IE 中尝试它时,它根本没有加载图像。这是我的代码:

    $("img.clickable").click( function() {
    $("#image_slider").animate({opacity:1.0,left:200},"slow");
    $("#image_container").attr("src",event.target.src);
    ihidden = false;
});

When I try running this in firefox or IE it just doesn't load the image at all. Any ideas? :)

当我尝试在 Firefox 或 IE 中运行它时,它根本不加载图像。有任何想法吗?:)

回答by alex

You need to define the eventin the arguments.

您需要event在参数中定义。

$("img.clickable").click( function(event) {
    $("#image_slider").animate({opacity:1.0,left:200},"slow");
    $("#image_container").attr("src",event.target.src);
    ihidden = false;
});

Otherwise it is going to use window.event.

否则它将使用window.event.

回答by Crayon Violent

try using $(this).attr('src')instead of event.target.src

尝试使用$(this).attr('src')代替event.target.src

回答by jwerre

Try this :

试试这个 :

target = (window.event) ? window.event.srcElement /* for IE */ : event.target

回答by samccone

$("img.clickable").click( function(e) { $("#image_slider").animate({opacity:1.0,left:200},"slow"); $("#image_container").attr("src",$(e.target).attr('src')); ihidden = false; });

$("img.clickable").click( function(e) { $("#image_slider").animate({opacity:1.0,left:200},"slow"); $("#image_container").attr("src",$(e.target).attr('src')); ihidden = false; });

This should work just fine

这应该工作得很好