对事件冒泡/捕获和 jQuery 感到困惑

发布于 2024-09-07 09:48:36 字数 842 浏览 9 评论 0原文

我对 jQuery 绝对是个菜鸟,在前端开发方面也很糟糕,所以这是我的问题。

使用以下 HTML

<select id="event_category_id" name="event_category_id">
  <option value="">Select Category</option>
  <option value="1">Category One</option>
  <option value="2">Category Two</option>
  <option value="3">Category Three</option>
</select>

和以下 jQuery,

$(document).ready(function() {
  // commands go here
  $('#event_category_id option').click( function(e) { 
    // e.stopImmediatePropagation();
    alert( $('#event_category_id option:selected').text() ); 
   });
});

只需单击一次,警报就会显示两次,每次都会显示正确的选择文本。通过取消注释 stopImmediatePropagation 可以纠正此问题。

所以我的问题是,如果这是事件冒泡/捕获,那么其中一个警报不会是空白的,因为选择没有文本,只有选项。

或者我是否认为所有这些工作的运作方式都在错误的轨道上?

感谢您抽出时间。

I'm an absolute noob with jQuery and pretty crap at frontend development so here's my question.

With the following HTML

<select id="event_category_id" name="event_category_id">
  <option value="">Select Category</option>
  <option value="1">Category One</option>
  <option value="2">Category Two</option>
  <option value="3">Category Three</option>
</select>

And the following jQuery

$(document).ready(function() {
  // commands go here
  $('#event_category_id option').click( function(e) { 
    // e.stopImmediatePropagation();
    alert( $('#event_category_id option:selected').text() ); 
   });
});

I get the alert being displayed twice, with the correct text of the selection each time, for one click. It's rectified by un-commenting the stopImmediatePropagation.

So my question is,if this is event bubbling/capturing, wouldn't one of the alerts be blank as the select doesn't have text, only the option.

Or am I on the wrong track with how I think all of this works works?

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀里藏娇 2024-09-14 09:48:36

不要使用click事件。请改用 change 事件。

$(document).ready(function() {  
   $('#event_category_id').change( function(e) { 
     alert( $(this).children('option:selected').text() ); 
   });
});

参考:更改

don't use the click event. Use the change event instead.

$(document).ready(function() {  
   $('#event_category_id').change( function(e) { 
     alert( $(this).children('option:selected').text() ); 
   });
});

Reference: change

别念他 2024-09-14 09:48:36

尝试...

$('#event_category_id').change( function(e) {    
   alert( $(this).find(':selected').text() ); 
});

玩一下演示

try...

$('#event_category_id').change( function(e) {    
   alert( $(this).find(':selected').text() ); 
});

play with the demo

守护在此方 2024-09-14 09:48:36

尝试将事件冒泡解释为一个广泛的主题。

事件对象提供了 .stopPropagation() 方法,该方法可以完全停止事件的冒泡过程。该方法是一个普通的 JavaScript 功能,但不能在所有浏览器上安全使用。但如果所有事件处理程序都使用 jQuery 正确注册,则可以使用该方法。

stopPropagation 阻止当前之外的任何对象接收事件,并且这可以在事件的任何阶段内。 stopImmediatePropagation 执行相同的操作,但还采取额外的步骤来防止接收事件的当前目标内发生其他事件。

默认操作

当使用具有默认操作的元素时,例如具有点击重定向的默认操作的元素。类似地,当用户在编辑表单时按下 Enter 键时,会触发表单上的提交事件,但在此之后实际上会发生表单提交。

如果不需要这些默认操作,则在事件上调用 .stopPropagation() 将无济于事。这些操作在事件传播的正常​​流程中不会发生。相反,.preventDefault() 方法将用于在触发默认操作之前停止事件。

要停止冒泡和默认操作,即调用 .stopPropogation() 和 .preventDefault() 方法,您只需返回 false

To try to explain bubbling of events as a broad topic.

The event object provides the .stopPropagation() method, which can halt the bubbling process completely for the event.this method is a plain JavaScript feature, but cannot be safely used across all browsers. But if all the event handlers are registered correctly using jQuery, you can use that method.

stopPropagation prevents any objects beyond the current from recieving the event, and this can be within any phase of the event. stopImmediatePropagation does the same but also takes the extra step of preventing additional events within the current target receiving the event from happening too.

Default Actions

When using elements that have default actions like which has a default action of redirecting on a click. Similarly, when the Enter key is pressed while the user is editing a form, the submit event is triggered on the form, but then the form submission actually occurs after this.

If these default actions are undesired, calling .stopPropagation() on the event will not help. These actions occur nowhere in the normal flow of event propagation. Instead, the .preventDefault() method will serve to stop the event in its tracks before the default action is triggered.

To stop both bubbling and default actions, i.e. to call the .stopPropogation() and the .preventDefault() methods, you simply return false;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文