【问题标题】:How to pass an inline js action into an external js file? [duplicate]如何将内联 js 动作传递到外部 js 文件中? [复制]
【发布时间】:2013-08-05 00:48:31
【问题描述】:

我对 JS 的了解相当有限,但我们在页面上有一个操作会导致外部 js 文件中的 if 语句。我正在使用(event.toElement),它适用于chrome,但不适用于ff 或opera。

内联js

    $("div#jquery-live-search .dialog-iframe-card").dialog2IFrame(  { 
            height:900,
              closeOnOverlayClick: true, 
              closeOnEscape: true, 
              removeOnClose: true, 
              showCloseHandle: true,
}); 

    $("div#jquery-live-search .dialog-iframe-report").dialog2IFrame(    { 
                height:900,
                  closeOnOverlayClick: true, 
                  closeOnEscape: true, 
                  removeOnClose: true, 
                  showCloseHandle: true,
    }); 
    }); 

外部js

var parentHtml = "";
            if ($(event.toElement).hasClass("dialog-iframe-card")) {
            parentHtml = $(__DIALOG_HTML_CARD);
            } else if ($(event.toElement).hasClass("dialog-iframe-report")) {
            parentHtml = $(__DIALOG_HTML_REPORT);
            }

parentHTML 在 js 文件的开头有一个变量,该变量在 if 语句进行切换时加载。在 chrome 以外的浏览器中工作时,我似乎无法弄清楚如何将正确的操作传递给外部文件。

我还尝试了以下仅适用于 chrome 的方法...

var $target = $(event.target);
                if ($target.hasClass("dialog-iframe-card")) {
                parentHtml = $(__DIALOG_HTML_CARD);
                } else if ($target.hasClass("dialog-iframe-report")) {
                parentHtml = $(__DIALOG_HTML_REPORT);
                }

建议的答案不能回答或解决问题。

【问题讨论】:

  • 看了一下,想不通。

标签: javascript jquery


【解决方案1】:

您应该使用 event.target 来找出触发事件的元素。看看这个http://api.jquery.com/event.target/

【讨论】:

  • 谢谢,还是迷路了
猜你喜欢
  • 2014-04-08
  • 2012-03-30
  • 2020-05-31
  • 2018-02-23
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多