简体   繁体   中英

Passing values to Jquery Function

here is my code,

$(document).ready(function(){

//if(event.type == 'mouseenter') { 

$(".big2box").mouseover(function() {


    var val1='.big2box';
    var val2='.small2box';

});

$(".big0box").mouseover(function() {


    var val1='.big0box';
    var val2='.small0box';

});

     $(val1 +","+ val2).hover(function(){
     alert(val1+val2)
     });`

now iam trying to pass the specific values for val1,val2 if an user mouseover on specific div `

iam aware we can do this by passing through function but not sure how it works in jquery.is there anyone who can help me to fix this.?

I guess you need something like this:

$(document).ready(function(){

var val1 = null;
var val2 = null;

$(".big2box").mouseover(function() {
    if (val1 != null && val2 != null){
        $(val1 +","+ val2).off("hover");
    }

    val1='.big2box';
    val2='.small2box';
    $(val1 +","+ val2).on("hover",function(){
       alert(val1+val2)
    });
});

$(".big0box").mouseover(function() {
    if (val1 != null && val2 != null){
        $(val1 +","+ val2).off("hover");
    }

    val1='.big0box';
    val2='.small0box';

    $(val1 +","+ val2).on("hover",function(){
       alert(val1+val2)
    });
});

Another way:

$(document).ready(function(){

//if(event.type == 'mouseenter') { 
var val1= null;
var val2= null;

$(".big2box").mouseover(function() {

val1='big2box';
val2='small2box';

});

$(".big0box").mouseover(function() {

val1='big0box';
val2='small0box';

});

     $(".big2box,.small2box,.big0box,.small0box").hover(function(){
         if ($(this).hasClass(val1) || $(this).hasClass(val2)){
            alert(val1+val2);
         }
     });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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