简体   繁体   English

同一元素的多次点击事件?

[英]Multiple click events for the same element?

Is it possible to have multiple click events for the same element? 同一元素是否可能有多个点击事件? I have tried to simply have it like so: 我试着简单地像这样:

$('#templates').click(function(e) {
            do something..
        });

$('#templates').click(function(e) {
            do something else also..
        });

Yet only the second event fires. 然而只有第二件事发生。 I cannot find any decent answers explaining how to do this for a singular element in an on-click? 我找不到任何合适的答案来解释如何在单击时针对单个元素执行此操作?

Note : the first click event calls server-side and loads a new PHP template (this may have an effect on what I can use in the second call I guess, as individually both clicks work but the server call does not work if I try a second click for the same element) 注意 :第一次点击事件会在服务器端调用并加载一个新的PHP模板(这可能会影响我在第二次调用中可以使用的功能,因为两次单击均有效,但如果尝试执行一次,则服务器调用将不起作用单击同一元素的第二次点击)

$('#templates').click(function(e) {
        functionOne();
        functionTwo();
});

function functionOne(){

}

function functionTwo(){

}

perhaps? 也许?

Please check this fiddle: 请检查此小提琴:

http://jsfiddle.net/DqSSd/ http://jsfiddle.net/DqSSd/

As you can see it should work well. 如您所见,它应该运作良好。

So please provide more information, and it would be better, if you provide JS fiddle as well. 因此,请提供更多信息,如果您还提供JS小提琴,那会更好。

Because so far the problem might be in: 因为到目前为止,问题可能出在:

  • second event is fired before first event returns the result 在第一个事件返回结果之前触发第二个事件
  • first event returns error from the server 第一个事件从服务器返回错误
  • some of events contains syntax error 一些事件包含语法错误
  • etc 等等

You may check something of those with investigation of NET calls to server (with Firebug or Chrome Developer toolbar). 您可以通过调查对服务器的NET调用(使用Firebug或Chrome Developer工具栏)来检查其中的某些内容。

Also for testing purposes you can type in console $('#templates').data("events") , so you will be able to see all events and handlers for particular element. 同样出于测试目的,您可以输入控制台$('#templates').data("events") ,因此您将能够查看特定元素的所有事件和处理程序。

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

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