如何使用事件冒泡取消

发布于 2024-09-18 09:54:53 字数 2084 浏览 5 评论 0原文

我到处寻找有关如何阻止事件冒泡发生的代码,并且我从 Quirksmode 网站上找到了一个代码,如下所示:

function doSomething(e){
    if(!e) var e = window.event;
    e.cancelBubble = true;
    if(e.stopPropagation) e.stopPropagation();
}

但我不知道如何以及在哪里使用它。 'e' 参数用作什么(或者应该作为 'e' 传递什么)? 是否要在事件处理程序代码中调用此函数? ...ETC?

我需要一些帮助,请问有人可以给我一些提示吗?

基本上我有 4 个元素,它们有一个名为“updateAvailableAttributes()”的“onchange”处理程序,如下所示:

<select id="deliveryMethod" name="deliveryMethod" onchange="updateAvailableAttributes();"></select>

<select id="formatMethod" name="formatMethod" onchange="updateAvailableAttributes();"></select>

<select id="yearsMethod" name="yearsMethod" onchange="updateAvailableAttributes();"></select>

<select id="updateMethod" name="updateMethod" onchange="updateAvailableAttributes();"></select>

这是 updateAvailableAttributes() 脚本:

function updateAvailableAttributes() {
var form = document.forms["orderDefinition"];
form.elements["formChangeRequest"].value = "true";
$.ajax({
  type: "POST",
  url: "ajax/possibleValues.html",
  data: $("form#orderDefinition").serialize(),
  success: function(response){
    $('#usercontent .sleeve .toprow').html(response);

    applyValidation();
    radioButtonHighlightSelection();

  },
  error: function(response, ioArgs) {
         if (response.status == 601) {
             sessionTimedOut();
         } 
      }
});

// Display a "please wait" message
$("#waitingMsgOverlay, #waitingMsgBox, #waitingMsg, #waitingMsgParag").ajaxStart(function(){
      var map = document.getElementById("OrderMap");
      map.disableApplication();
      $(this).show();
      radioButtonHighlightSelection();
  }).ajaxStop(function(){
      var map = document.getElementById("OrderMap");
      map.enableApplication();
      $(this).hide();
      $("#toolpanel").height($("#orderMap").height());
      radioButtonHighlightSelection();
});}

我的问题是,如何将“doSomething(e)”与“updateAvailableAttributes()”合并' 我已经使用了 'onchange' 事件处理程序?

先感谢您。

I've looked everywhere for a code on how to stop event bubbling to occur, and I've found one from the Quirksmode website, which is this:

function doSomething(e){
    if(!e) var e = window.event;
    e.cancelBubble = true;
    if(e.stopPropagation) e.stopPropagation();
}

But I don't know how and where to use it.
What is the 'e' parameter used as (or what should be passed as 'e')?
Is this function to be called in an event handler code?
...etc?

I need some help, please can someone give me some hint?

Basically I have 4 elements that have an 'onchange' handler called 'updateAvailableAttributes()', like this:

<select id="deliveryMethod" name="deliveryMethod" onchange="updateAvailableAttributes();"></select>

<select id="formatMethod" name="formatMethod" onchange="updateAvailableAttributes();"></select>

<select id="yearsMethod" name="yearsMethod" onchange="updateAvailableAttributes();"></select>

<select id="updateMethod" name="updateMethod" onchange="updateAvailableAttributes();"></select>

Here is the updateAvailableAttributes() script:

function updateAvailableAttributes() {
var form = document.forms["orderDefinition"];
form.elements["formChangeRequest"].value = "true";
$.ajax({
  type: "POST",
  url: "ajax/possibleValues.html",
  data: $("form#orderDefinition").serialize(),
  success: function(response){
    $('#usercontent .sleeve .toprow').html(response);

    applyValidation();
    radioButtonHighlightSelection();

  },
  error: function(response, ioArgs) {
         if (response.status == 601) {
             sessionTimedOut();
         } 
      }
});

// Display a "please wait" message
$("#waitingMsgOverlay, #waitingMsgBox, #waitingMsg, #waitingMsgParag").ajaxStart(function(){
      var map = document.getElementById("OrderMap");
      map.disableApplication();
      $(this).show();
      radioButtonHighlightSelection();
  }).ajaxStop(function(){
      var map = document.getElementById("OrderMap");
      map.enableApplication();
      $(this).hide();
      $("#toolpanel").height($("#orderMap").height());
      radioButtonHighlightSelection();
});}

My question is, how do I incorporate the 'doSomething(e)' with 'updateAvailableAttributes()' I have already on the 'onchange' event handler?

Thank you in advance.

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

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

发布评论

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

评论(3

一抹微笑 2024-09-25 09:54:53

整个 doSomething 就是事件处理程序本身。您只需像没有参数一样注册事件即可。 “e”参数由 JavaScript 运行时本身提供。

The whole doSomething is the event handler itself. You just register the event exactly as you would if there was no parameter. The "e" parameter is provided by the JavaScript runtime itself.

浅语花开 2024-09-25 09:54:53

e 是事件。例如,如果你有一个 div 位于另一个 div 中,并且它们都有一个名为 doSomething 的 js 单击处理程序。所以在 onclick 属性中使用 onclick="doSomething(event);"如果你点击内部 div 外部现在不会处理它

e is the event. For example if u have a div inside another div and both of them have a js click handler called doSomething. so in the onclick attribute use onclick="doSomething(event);" if you click on the inner div outer will not handle it now

情泪▽动烟 2024-09-25 09:54:53

在 DOM 模型中,有多种与元素相关的事件,例如 onclick。如果您想处理任何事件,请将事件侦听器附加到元素。例如 element.addEventListner(event,yourfunction,bubble)

请参阅此http://www.quirksmode.org/js/events_advanced.html

In DOM model there are various events associated with an element e.g. onclick. If you want to handle any event you attach an event listener to an element. e.g. element.addEventListner(event,yourfunction,bubble).

see this http://www.quirksmode.org/js/events_advanced.html

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