如何将内联js操作传递到外部js文件中

How to pass an inline js action into an external js file?

本文关键字:js 外部 文件 操作      更新时间:2023-09-26

我对JS的了解相当有限,但我们在页面上有一个操作,导致在外部JS文件中出现if语句。我使用的是(event.toElement),它可以在chrome中使用,但不能在ie、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以外的浏览器中工作时将正确的操作传递给外部文件。

我还尝试了以下只对铬有效的方法。。。

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);
                }

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

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