在jQuery中传递变量 [英] Passing variables in jquery
本文介绍了在jQuery中传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将var full传递给函数b的最佳方法是什么.我不想使用全局变量.是返回的唯一选择.
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.
<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>
推荐答案
您可以通过如下定义变量来将变量传递给函数:
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);
});
这篇关于在jQuery中传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文