$(window).resize函数奇怪的行为

Sam*_*ong 1 javascript jquery

mouseenter如果语句$("."+ElementID+"-delta-ui-dropdown-appendHere").attr('style') == 'display: block;'){为false ,我有一个从元素中删除类的事件.

它完美地工作,直到我添加了一个通过调整浏览器大小触发的事件.最初加载时,mouseenter事件工作正常,但是当我调整浏览器大小时,mouseenter即使条件为真,事件也会删除该类.

如果我删除$(window).resize代码,该mouseenter事件将再次起作用.

//this is the code triggered

var getWidth = $("."+ElementID+"-delta-ui-dropdown-appendHere").outerWidth();

$(window).resize(function() {
    // This will execute whenever the window is resized
    if ($(window).width() >= 992) {
        console.log('original');
        $(".delta-ui-dropdown-common-"+ElementID+"").css({width:getWidth});
    } else {
        var inputGroupWidth = $(".delta-ui-dropdown-"+ElementID+"").width();
        $(".delta-ui-dropdown-common-"+ElementID+"").css({width:inputGroupWidth});
        console.log('fit');
    }
});



//this code removes the class even though the condition is true

$('.delta-ui-dropdown-icon-'+ElementID).mouseenter(function() {
    if ($("."+ElementID+"-delta-ui-dropdown-appendHere").attr('style') == 'display: block;') {

    } else {
        $('.delta-ui-dropdown-icon-'+ElementID).removeClass('focus');
    }   
});
Run Code Online (Sandbox Code Playgroud)

Gur*_*Rao 5

你的if病情永远如此false.尝试使用css如下:

$('.delta-ui-dropdown-icon-'+ElementID).mouseenter(function() {
    if($("."+ElementID+"-delta-ui-dropdown-appendHere").css('display') == "block") {

    } else {
        $('.delta-ui-dropdown-icon-'+ElementID).removeClass('focus');
    }   
});
Run Code Online (Sandbox Code Playgroud)