简体   繁体   English

无法使用jQuery .trigger触发click事件

[英]unable to trigger click event using jQuery .trigger

I am having some trouble in triggering a click event . 我在触发点击事件时遇到了一些麻烦。

I have html something like this : 我有这样的HTML:

<div class="carousel-control" >
<a href='#' id="carousel_1">1</a>
<a href='#' id="carousel_2">2</a>
<a href='#' id="carousel_3">3</a>
<a href='#' id="carousel_4">4</a>
<a href='#' id="carousel_5">5</a> 
etc
</div>

Now Here is the JS 现在这是JS

jQuery('.carousel-control a').bind('click', function() {
        alert("carousel cliced")
        return false;
      });

$('#nextimg').click(function() {

var car_index=2;
                $('#carousel_'+(car_index+1)).trigger('click');
alert("next image clicked");
               return false;
            });

Now when I manually click the href tag the alert("carousel clicked") appers, but when I click the nextimg only next image alert appears not the carousel alert. 现在,当我手动点击href标签时,警报(“carousel clicked”)出现,但是当我点击nextimg时,只显示下一个图像警报而不是轮播警报。

I tried using 我试过用

 $('#carousel_2').trigger('click'); 
 $('#carousel_2').click();
 $('#carousel_2').triggerHandler('click');

There seems to be no error in the firebug too. 萤火虫似乎也没有错误。

Can someone please help me out. 有人可以帮帮我吗。 :) Thank you, :) 谢谢,

I'm not certain... but any chance it has to do with your spelling of index in var car_inedx=2; 我不确定......但是有任何机会与var car_inedx=2;index拼写有关var car_inedx=2; ? (I make mistakes like this often enough myself.) (我自己经常犯这样的错误。)

Keep the click event under "Document Ready event". 将Click事件保留在“Document Ready事件”下。

Here it goes like this : 这是这样的:

jQuery(document).ready( function() {
      jQuery('.carousel-control a').bind('click', function() {
         alert("carousel cliced")
         return false;
       });

       $('#nextimg').click(function() {
             var car_index=2;
             $('#carousel_'+(car_index+1)).trigger('click');
             alert("next image clicked");
             return false;
       });
});

Try 尝试

$('#carousel_'+(car_index+1)).click();

instead of 代替

$('#carousel_'+(car_index+1)).trigger('click');

You have a typo in var car_inedx . 你在var car_inedx

Working demo: http://jsfiddle.net/rBFxu/ 工作演示: http//jsfiddle.net/rBFxu/

using 运用

 $('.carousel-control a').bind('click', function()

instead of this 而不是这个

jQuery('.carousel-control a').bind('click', function()

Works well. 效果很好。

But can anyone explain me why is it happening so ? 但任何人都可以解释我为什么会这样发生?

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

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