Passing variables in jquery

后端 未结 4 1967
小鲜肉
小鲜肉 2021-01-25 04:52

What is the best way to pass the var full to function b. I don\'t want to use global variables. Is return the only option.



        
4条回答
  •  滥情空心
    2021-01-25 05:15

    You can pass a variable to a function by defining it as follows:

    function b(x) {
        alert(x);
    }
    

    and then calling it by stating its name and passing a variable as the argument:

    b(full);
    

    So in the context of your code:

    $("body").click(function(e){
        var name = $(e.target)[0].nodeName;
        var nameid = $(e.target)[0].id;
        var classname = $(name+"#"+nameid).attr('class');
        var full = name+"#"+nameid;
        console.log(nameid);
    
        function b(x){
            alert(x);
        };
    
        b(full);
    });
    

提交回复
热议问题