Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何组合这些jQuery.on()函数?_Javascript_Jquery_Dom_Modal Dialog - Fatal编程技术网

Javascript 如何组合这些jQuery.on()函数?

Javascript 如何组合这些jQuery.on()函数?,javascript,jquery,dom,modal-dialog,Javascript,Jquery,Dom,Modal Dialog,这是我的密码。滑动用于文档中的任何位置,单击用于关闭img,但它们执行相同的代码。有什么建议吗?这不是什么大不了的事,但如果可以的话,我希望代码更小、更干净。这适用于模态类型窗口 $(document).on("click", "#close", function () { ttl.html(docTitle + air); window.history.pushState({}

这是我的密码。滑动用于文档中的任何位置,单击用于关闭img,但它们执行相同的代码。有什么建议吗?这不是什么大不了的事,但如果可以的话,我希望代码更小、更干净。这适用于模态类型窗口

                 $(document).on("click", "#close", function () {
                     ttl.html(docTitle + air);
                     window.history.pushState({}, docTitle + air, docURL);
                 }).on("swipeleft swiperight", function () {
                     ttl.html(docTitle + air);
                     window.history.pushState({}, docTitle + air, docURL);
                 });

这里没有理想的解决方案,因此您可以简单地执行以下操作:

var f = function () {
      ttl.html(docTitle + air);
      window.history.pushState({}, docTitle + air, docURL);
}
$(document).on("click", "#close", f).on("swipeleft swiperight", f);
如果您在全局范围内或在大范围内执行此操作,您可以将整个生命包含在一个生命中,以保持外部范围更干净:

(function(){
    var f = function () {
          ttl.html(docTitle + air);
          window.history.pushState({}, docTitle + air, docURL);
    }
    $(document).on("click", "#close", f).on("swipeleft swiperight", f);
})();

这里没有理想的解决方案,因此您可以简单地执行以下操作:

var f = function () {
      ttl.html(docTitle + air);
      window.history.pushState({}, docTitle + air, docURL);
}
$(document).on("click", "#close", f).on("swipeleft swiperight", f);
如果您在全局范围内或在大范围内执行此操作,您可以将整个生命包含在一个生命中,以保持外部范围更干净:

(function(){
    var f = function () {
          ttl.html(docTitle + air);
          window.history.pushState({}, docTitle + air, docURL);
    }
    $(document).on("click", "#close", f).on("swipeleft swiperight", f);
})();

您可以创建一个这样做的函数并调用它

function foo(ttl, docTitle, air, docURL) {
   ttl.html(docTitle + air);
   window.history.pushState({}, docTitle + air, docURL);
}
从on语句中调用它

$(document).on("click", "#close", 
        foo(ttl, docTitle, air, docUrl)).
    on("swipeleft swiperight", 
        foo(ttl, docTitle, air, docUrl));

您可以创建一个这样做的函数并调用它

function foo(ttl, docTitle, air, docURL) {
   ttl.html(docTitle + air);
   window.history.pushState({}, docTitle + air, docURL);
}
从on语句中调用它

$(document).on("click", "#close", 
        foo(ttl, docTitle, air, docUrl)).
    on("swipeleft swiperight", 
        foo(ttl, docTitle, air, docUrl));

有趣的我知道一定有更酷的方法;)-谢谢,很有趣!我知道一定有更酷的方法;)-非常感谢。