jQuery:调用 click(); 从另一个点击();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1968480/
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-08-26 12:30:08  来源:igfitidea点击:

jQuery: call click(); from another click();

jqueryclick

提问by dr3w

Is it possible to do something like this

是否有可能做这样的事情

$('#idone').click(function(){
 alert('we do stuff!');
});

$('#idtwo').click(function(){
 $('#idone').click();
});

... i guess not, but are there any possible workarounds?

...我想不是,但有没有可能的解决方法?

!UPD:

!UPD:

Well, ok. It's a little bit more complicated. I have a jCarousel analog installed - JQueryTOOLS - Scrollable - I guess that's what its name the link: http://flowplayer.org/tools/demos/scrollable/gallery.html

好吧,好吧。这有点复杂。我安装了一个 jCarousel 模拟 - JQueryTOOLS - Scrollable - 我想这就是它的名字链接:http: //flowplayer.org/tools/demos/scrollable/gallery.html

I presume that it has click() event somehow hardcoded in it and when i click thumbs the plugin scrolls it to center position. Then I've bound another click event, so that I can display large image. It works similar to the example in the link above

我认为它以某种方式硬编码了 click() 事件,当我单击拇指时,插件会将其滚动到中心位置。然后我绑定了另一个点击事件,这样我就可以显示大图了。它的工作原理类似于上面链接中的示例

Now I have to make this big image clickable, so clicking it I can proceed to next image in gallery, I did that, but now I have to make the carousel scroll automatically to the next thumb when I am clicking this big image. So, if I simplify my code, the essence is still smth like that:

现在我必须使这个大图像可点击,所以点击它我可以继续画廊中的下一个图像,我做到了,但是现在我必须在我点击这个大图像时让轮播自动滚动到下一个拇指。所以,如果我简化我的代码,本质仍然是这样:

$('#idone').click(function(){
 alert('we do stuff!');
});

$('#idtwo').click(function(){
 $('#idone').click();
});

where #idone is a thumb in the gallery, #idtwo is that preview image. I dont know what function is bound to click by default. Another thing: my guess was that binding the same event to the same object should replace the previous - it appears not necessarily.. And I'm pretty sure that it is exactly click();, not mouseup\down etc. cause calling

其中#idone 是图库中的拇指,#idtwo 是预览图像。我不知道默认情况下绑定什么功能。另一件事:我的猜测是将相同的事件绑定到相同的对象应该替换前一个 - 它似乎不一定......而且我很确定它正是 click();,而不是 mouseup\down 等导致调用

$('#idone').click();

on page load does the trick and carousel scrolls to the thumb with id="idone"

在页面加载时,旋转木马滚动到 id="idone" 的拇指

just in case: jquery-1.3.2.js

以防万一:jquery-1.3.2.js

thanks everyone for your answers, and excuse me my lame English, no time for brushing up (:

谢谢大家的回答,请原谅我的英语蹩脚,没时间复习(:

!UPD:

!UPD:

ok! so I gave up on this one )) But to close this question and put an end to this discussion i have to know. All you people, who wrote that it is possible and suggested variants. HAVE YOU TRIED THEM?! Do they really work for you? Is it me, who is wrong? And - Thanks anyway!

好的!所以我放弃了这个 )) 但是要结束这个问题并结束这个讨论,我必须知道。所有写过这是可能的并建议变体的人。你试过吗?!他们真的为你工作吗?是我,谁错了?而且 - 无论如何谢谢!

回答by RamboNo5

This would be another possiblity:

这将是另一种可能性:

$('#idone').click(function(){
 alert('we do stuff!');
});

$('#idtwo').click(function(){
 $('#idone').trigger('click');
});

edit:I just see that your code should work like that already.

编辑:我只是看到你的代码应该已经这样工作了。

回答by Groo

Define a separate function which is called by both events?

定义一个由两个事件调用的单独函数?

function doSomething() {
 alert('we do stuff!');
}

$('#idone').click(doSomething);    
$('#idtwo').click(doSomething);

or simply

或者干脆

$('#idone, #idtwo').click(doSomething);

[EDIT - jQuery Scrollable]

[编辑 - jQuery 可滚动]

Have you tried using this with the thumbnails in jQuery Scrollable:

您是否尝试过将其与 jQuery Scrollable 中的缩略图一起使用:

$('#idone').next(); 

(I presume #idone is scrollable)

(我认为 #idone 是可滚动的)

回答by cletus

Actually calling:

实际调用:

$("#idone").click();

will do what you want: call the click()event handlers on #idone.

会做你想做的事:在 上调用click()事件处理程序#idone

That being said, I would advise splitting it out into a separate function as chaining event handlers like this has the potential to confuse someone else reading the code. For example:

话虽如此,我建议将其拆分为一个单独的函数,因为像这样链接事件处理程序有可能混淆其他人阅读代码。例如:

$('#idone').click(idone_click);
$('#idtwo').click(idtwo_click);

function idone_click() {
  alert("id one click");
}

function idtwo_click() {
  idone_click();
}

Of course, if you simply want them both to do the same thing you can do this:

当然,如果你只是想让他们都做同样的事情,你可以这样做:

$("#idone, #idtwo").click(function() {
  alert(this.id + " clicked");
});

回答by RamboNo5

I see. Then you may want to do something like this.

我知道了。那么你可能想要做这样的事情。

$('#image_wrap img').click(function(){ // big image is clicked
    // figure out the active element of the items below
    $active_element = $('.scrollable .items .active');

    // move along to the element to its right
    $nextone = $active_element.next();

    // trigger a click on this element
    $nextone.trigger('click');
});

That way, when you click the big image, a click event on the next preview image in the list is triggered.
(If that element doesn't exist, jQuery is nice and does nothing - no errors.)

这样,当您单击大图像时,将触发列表中下一个预览图像的单击事件。
(如果该元素不存在,jQuery 很好,什么也不做 - 没有错误。)

回答by TarangP

You can do that simply By Jquery Trigger.it Execute all handlers and behaviors attached to the matched elements for the given event type. As according to your example

您可以简单地通过Jquery Trigger来做到这一点。它执行附加到给定事件类型的匹配元素的所有处理程序和行为。根据你的例子

$('#idone').click(function(){
 alert('I Am First Button');
});

$('#idtwo').click(function(){
 //THIS WILL CALL FIRST METHOD
 $('#idone').trigger('click');
 alert("I Am Second One .!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="idone">PASS 1</span>
<br>
<button id="idtwo">PASS 2</span>