简体   繁体   English

2单击一个元素上的事件,只需要运行一个元素

[英]2 click events on one element, only one needs to run

I have 2 click events on one element (button). 我在一个元素(按钮)上有2个点击事件。 The first is a default function to do some general checks (which are be done on general steps in my proces) and the second one is a specific check for that specific step in my proces. 第一个是默认函数,用于执行一些常规检查(在我的过程中的一般步骤中完成),第二个是对我的过程中特定步骤的特定检查。

All the steps in my proces have a next button to go to the next step (next page), but if a step need specific checks (field validation), I give the <div> around it an extra class, so I can handle it with jQuery. 我的过程中的所有步骤都有一个下一步按钮进入下一步(下一页),但如果一个步骤需要特定的检查(字段验证),我给它周围的<div>一个额外的类,所以我可以处理它用jQuery。

Default step next button 默认步骤下一步按钮

HTML: HTML:

<div>
   <a class="next-button" href="http://www.example.com/to-next-step/">Next step</a>
</div>

jQuery: jQuery的:

$('.next-button').click(function() {
   // Default checks
});

Specific step next button 具体步骤下一步按钮

HTML: HTML:

<div class="specific-step">
   <a class="next-button" href="http://www.example.com/to-next-step/">Next step</a>
</div>

jQuery: jQuery的:

$('.specific-step .next-button').click(function() {
   // Specific checks
});

When I click the button on a step with an extra class around it, both functions are running. 当我单击一个带有额外类的步骤上的按钮时,两个函数都在运行。 Is it possible to only run the "specific next button function" then? 是否可以只运行“特定的下一个按钮功能”呢?

Test if the parent has the specific step, from inside the single generic click handler attached to all .next-button : 从附加到所有.next-button的单个通用单击处理程序内部测试父级是否具有特定步骤:

$('.next-button').click(function() {
    if($(this).parent().hasClass('specific-step')){
         performSpecificStep();
    }
    else{
        // Do standard stuff
    }
});

An alternative is to use e.stopImmediatePropagation() , but this solution requires your "specific" handlers to be attached first as they execute in the order they are attached: 另一种方法是使用e.stopImmediatePropagation() ,但是这个解决方案需要首先附加“特定”处理程序,因为它们按照它们所附加的顺序执行:

eg 例如

$('.specific-step .next-button').click(function (e) {
    e.stopImmediatePropagation();
});
$('.next-button').click(function (e) {
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/nb6thezu/ JSFiddle: http //jsfiddle.net/TrueBlueAussie/nb6thezu/

Add a new class to the specific elements (eg specific-step) 向特定元素添加新类(例如,特定步骤)

Default Button 默认按钮

<a class="next-button" href="http://www.example.com/to-next-step/">Next step</a>

Specific Button 具体按钮

<a class="next-button specific-step" href="http://www.example.com/to-next-step/">Next step</a>

And your jquery: 你的jquery:

$('.next-button').click(function() {
    if($(this).hasClass('specific-step')){
         performSpecificStep();
    }else{
      // do normal stuff
    }
});

Since you want entirely separate functionality for each type of link, I would identify each type of link with a distinct class. 由于您希望为每种类型的链接提供完全独立的功能,因此我会识别具有不同类的每种类型的链接。

<a class="next-button default-step" ... <a class="next-button specific-step" ...

Keep your CSS styles in next-button , then attach event handlers to default-step and specific-step . 将CSS样式保存next-button ,然后将事件处理程序附加到default-stepspecific-step

Then you can avoid: 然后你可以避免:

  1. Hard coding CSS classnames into your event handlers 将CSS类名硬编码到事件处理程序中
  2. Embedding different functions into a single function using if ... else 使用if ... else不同的函数嵌入到单个函数中

My gut says this design will be easier to change in future. 我的直觉说这个设计将来会更容易改变。

edit: If only one of these buttons is ever present on a page, perhaps using an ID instead of a class makes more sense. 编辑:如果页面上只出现其中一个按钮,则使用ID而不是类更有意义。 Just a thought. 只是一个想法。

Try this. 试试这个。 the ">" makes sure you bind the click function to the child of "specific-step". “>”确保将click函数绑定到“specific-step”的子节点。

$('.specific-step > .next-button').click(function() {
   // Specific checks
});

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

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