简体   繁体   English

如何使用jQuery定义函数参数

[英]How to define function parameter with jQuery

i have some syntax problem... This is my little script: 我有一些语法问题...这是我的小脚本:

$('.basket_details, .advanced_search_panel, .producers_major_panel').hover(function () {
    mouse_is_inside = true;
}, function () {
    mouse_is_inside = false;
});
$("body").mouseup(function () {
    if (mouse_is_inside) {
        $('.advanced_search_panel, .producers_major_panel').fadeOut('slow');
        $('.basket_details').slideUp('slow');
    }
});

everything's fine but my chrome console screaming that i have syntax errors, and mouse_is_inside is not define, how to correct this mistake? 一切都很好,但是我的chrome控制台尖叫我有语法错误,并且mouse_is_inside没有定义,如何纠正这个错误?

Try a closure: 尝试关闭:

(function() {

  var mouse_is_inside = false;

$('.basket_details, .advanced_search_panel, .producers_major_panel').hover(function () {
    mouse_is_inside = true;
}, function () {
    mouse_is_inside = false;
});
$("body").mouseup(function () {
    if (mouse_is_inside) {
        $('.advanced_search_panel, .producers_major_panel').fadeOut('slow');
        $('.basket_details').slideUp('slow');
    }
});

})();

This will allow you use of 'mouse_is_inside' within the scope of the functions. 这将允许您在功能范围内使用'mouse_is_inside'。 It also stops you from needing to use a global. 这也使您无需使用全局变量。

You have to add 您必须添加

var mouse_is_inside = false;

at the beginning of your code to mark it as a global variable. 在代码的开头将其标记为全局变量。

Your mouse_is_inside variable isn't global. 您的mouse_is_inside变量不是全局变量。 You need to define it outside of your functions for both sets to be able to access it. 您需要在函数之外定义两个集合才能访问它。

Example: 例:

var mouse_is_inside = false;

$('.basket_details, .advanced_search_panel, .producers_major_panel').hover(function () {
    mouse_is_inside = true;
}, function () {
    mouse_is_inside = false;
});
$("body").mouseup(function () {
    if (mouse_is_inside) {
        $('.advanced_search_panel, .producers_major_panel').fadeOut('slow');
        $('.basket_details').slideUp('slow');
    }
});

Define variable mouse_is_inside in global window scope . 全局窗口范围内定义变量mouse_is_inside。 So it can be accessible in all the functions. 因此可以在所有功能中访问它。

or try with Closure as suggested by @Lloyd if you need a restricted scope 如果您需要限制范围,请尝试使用@Lloyd建议的关闭

Try This: 尝试这个:

$(document).on({
    mouseenter: function () {
        mouse_is_inside = true;
    },

    mouseleave: function () {
        mouse_is_inside = false;
    }
}, '.basket_details, .advanced_search_panel, .producers_major_panel');

$("body").mouseup(function () {
    if (mouse_is_inside) {
        $('.advanced_search_panel, .producers_major_panel').fadeOut('slow');
        $('.basket_details').slideUp('slow');
    }
});

The use of the variable mouse_is_inside is not needed at all in this example (unless you do other things with it in other functions its best to remove it altogether as its less efficient and it only clutters the code) in any-case don't use it in your example just call another function in the following way 在此示例中,根本不需要使用变量mouse_is_inside(除非您在其他函数中对其进行其他操作,最好是将其完全删除,因为它的效率较低,并且只会使代码混乱),无论如何都不要使用在您的示例中,只需按以下方式调用另一个函数

$(document).ready(function(){
$('.basket_details, .advanced_search_panel, .producers_major_panel').hover(function () {
            /*add the mouse_is_inside=false; here if you need it for other functionality but dont use it for hide your panels*/
    fold(); /*calling this function works just like your example but it fixes the problem and makes you use 1 less variable which is better*/
});
function fold(){
$("body").mouseup(function () {
        $('.advanced_search_panel, .producers_major_panel').fadeOut('slow');
        $('.basket_details').slideUp('slow');
});
}

});

you can also achieve the same functionality using click because you have to hover over an element before you click it. 您还可以使用click实现相同的功能,因为您必须先将鼠标悬停在某个元素上,然后再单击它。 (there might be times where this is not true if other function calls click etc, but hopefully in your example it will still do the job) (如果其他函数调用click等,有时可能不正确,但是希望在您的示例中它仍然可以完成此工作)

$('.basket_details, .advanced_search_panel, .producers_major_panel').click(function(){
        $('.advanced_search_panel, .producers_major_panel').fadeOut('slow');
        $('.basket_details').slideUp('slow');
});

Gl Łukasz, GlŁukasz,

Niech Moc będzie z tobą Niech Mocbędzieztobą

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM