在 jquery 中传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4611190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:34:01  来源:igfitidea点击:

Passing variables in jquery

jqueryvariables

提问by Hussein

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.

将 var full 传递给函数 b 的最佳方法是什么?我不想使用全局变量。是退货的唯一选择。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> 

<style> 
    .aaa, .bbb, .ccc { width:100px; height:100px;background-color:#ccc;margin-bottom:5px;}
</style>
<body class="123">

    <p id="ptest"></p>
    <p class="aaa" id="aaaid"></p>
    <p class="bbb" id="bbbid"></p>
    <p class="ccc" id="cccid"></p>
    <div></div>
</body>
<script type="text/javascript">
    $("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(){
            alert(full);
        };
    });
</script>

回答by Marcus Whybrow

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);
});

回答by Patrick Evans

Unless you dont want function b to exist, or used outside outside of the click function, i would move it outside the anonymous function. And then just specify the arguments in the definition then just pass them like normal

除非您不希望函数 b 存在,或者在单击函数之外使用,否则我会将其移到匿名函数之外。然后只需在定义中指定参数,然后像往常一样传递它们

Example

例子

<script type="text/javascript">
    $("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);
        b(full);
    });
    function b(full)
    {
        alert(full);
    };
</script>

回答by jon_darkstar

argument? is there something im missing here?

争论?我这里有什么遗漏吗?

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

call with

打电话给

b(full);

回答by jmort253

    // define b
    function b(full) {
        alert(full);
    }

Actually, you aren't using global variables. You're defining them inside the JQuery anonymous function bound to the body element.

实际上,您没有使用全局变量。您在绑定到 body 元素的 JQuery 匿名函数中定义它们。