开发者

Check if String Variable is a certain string value

I have a psuedo contextual menu script on my webpage. The idea is that a script will check to see if the element you're hovering over has a certain class. If it does, it sets a string variable to a certain value. This way, when ctrl is pressed, i can check the string variable content to determine which contextual menu should be un-hidden.

var cmEl = "";

$('div').live('mouseover', function(e){
    e.stopPropagation();

    var actEl = $(this);

    if (actEl.hasClass("B_Info")) {
        var cmEl = "BiP";
    } else if (actEl.hasClass("BiO")) {
        var cmEl = "BiO";
    } else if (actEl.hasClass("myOpt")) {
        var cmEl = "myOpt";
    } else {
        var cmEl = "GEN";
    }
    $("#tell").html("" + cmEl + "");

});

$(document).keydown(function(e) {
    if (e.ctrlKey) {
        if (cmEl.match('BiP')) {        
          开发者_开发知识库  $("#Badge_C_M").removeClass("HIDE");            
            $(this).remove();
        } else if (cmEl === "BiO") {        
            $("#Opt_C_M").removeClass("HIDE");          
        } else if (cmEl === "myOpt") {      
            $("#Count_C_M").removeClass("HIDE");            
        } else {
            $("#Gen_C_M").removeClass("HIDE");          
        }
    }
});

Via the html read-out, i can see that the variable is indeed being set to the desired values, but the latter part, that determines which menu to show, doesn't work.

As well, my first script only checks divs, but i'd like it to check any element type; or at least divs and imgs.


You're resetting the value of cmEl in your function scope. You need to just rewrite it like this....

if (actEl.hasClass("B_Info")) {
    cmEl = "BiP";
} else if (actEl.hasClass("BiO")) {
    cmEl = "BiO";
} else if (actEl.hasClass("myOpt")) {
    cmEl = "myOpt";
} else {
    cmEl = "GEN";
}

Now your global variable cmEl is being set, thus can be read in your subsequent .keydown function. If you want to read different elements, you can bind them like this....

$('div, img, input').mouseover(function(e){ ...code... });

hope this helps.


If you want to select all elements use $('*'). Also in your script do not use

var cmEL="..."

everytime you asign the value. "Var" is causing the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜