如何在jQuery中的匿名函数中获取变量?
How to get variables in an anonymous function in jQuery?
如何将变量传递给匿名函数。我想将几个变量传递给匿名函数,基于该函数,它将创建一个新字符串。
在此代码中,我想传递 url、时间戳、ID 和计划。
<script>
jQuery(document).ready(function() {
console.log("check")
var newUrl=url+'?id='+id+'×tamp='+timestamp+'&plan='+plan;
console.log(newUrl);
createStoryJS({
type: 'timeline',
width: '1250',
height: '240',
source: newUrl,
embed_id: 'my-timeline'
});
});
</script>
就绪处理程序的参数由 jQuery 传递并设置为 jQuery 对象(请参阅 https://api.jquery.com/ready/ > Aliasing the jQuery Namespace)。所以你不能把它传递到你上面代码中的函数声明中。
您可以将它设置为一个全局对象或设置一个表单字段,然后从您的函数内部读取它。 Fiddle 对于后者 - http://jsfiddle.net/eqz7410c/
HTML
<form>
<input id="b" type="hidden" value="123" />
</form>
JS
$(document).ready(function() {
alert($("#b").val())
});
您可以声明一个具有全局作用域的变量,并在函数调用中使用它,如下所示
var globalVar = 1;
jQuery(document).ready(function() {
console.log(globalVar);
});
首先,jQuery(document).ready(function() {});
将是文档准备好被访问时的入口点。有几种方法可以做到这一点。
想法是您不需要传递任何东西,只需使用您在此匿名函数中创建的资源。
I want to pass few variables to an anonymous function, based on the
function, it will create a new string.
我不建议您使用全局变量。您可能从中获得 id
、timestamp
和 plan
值的函数应该 return 您可以将其分配给 newUrl
inside document ready功能。您也可以使用 closure。
function returnUrl(){
// code to calculate id, timestamp and path..
// ....
// ....
return url+'?id='+id+'×tamp='+timestamp+'&plan='+plan;
}
jQuery(document).ready(function() {
// DOM ready to access..
console.log("check")
var newUrl = returnUrl();
console.log(newUrl);
createStoryJS({
type: 'timeline',
width: '1250',
height: '240',
source: newUrl,
embed_id: 'my-timeline'
});
});
如何将变量传递给匿名函数。我想将几个变量传递给匿名函数,基于该函数,它将创建一个新字符串。 在此代码中,我想传递 url、时间戳、ID 和计划。
<script>
jQuery(document).ready(function() {
console.log("check")
var newUrl=url+'?id='+id+'×tamp='+timestamp+'&plan='+plan;
console.log(newUrl);
createStoryJS({
type: 'timeline',
width: '1250',
height: '240',
source: newUrl,
embed_id: 'my-timeline'
});
});
</script>
就绪处理程序的参数由 jQuery 传递并设置为 jQuery 对象(请参阅 https://api.jquery.com/ready/ > Aliasing the jQuery Namespace)。所以你不能把它传递到你上面代码中的函数声明中。
您可以将它设置为一个全局对象或设置一个表单字段,然后从您的函数内部读取它。 Fiddle 对于后者 - http://jsfiddle.net/eqz7410c/
HTML
<form>
<input id="b" type="hidden" value="123" />
</form>
JS
$(document).ready(function() {
alert($("#b").val())
});
您可以声明一个具有全局作用域的变量,并在函数调用中使用它,如下所示
var globalVar = 1;
jQuery(document).ready(function() {
console.log(globalVar);
});
首先,jQuery(document).ready(function() {});
将是文档准备好被访问时的入口点。有几种方法可以做到这一点。
想法是您不需要传递任何东西,只需使用您在此匿名函数中创建的资源。
I want to pass few variables to an anonymous function, based on the function, it will create a new string.
我不建议您使用全局变量。您可能从中获得 id
、timestamp
和 plan
值的函数应该 return 您可以将其分配给 newUrl
inside document ready功能。您也可以使用 closure。
function returnUrl(){
// code to calculate id, timestamp and path..
// ....
// ....
return url+'?id='+id+'×tamp='+timestamp+'&plan='+plan;
}
jQuery(document).ready(function() {
// DOM ready to access..
console.log("check")
var newUrl = returnUrl();
console.log(newUrl);
createStoryJS({
type: 'timeline',
width: '1250',
height: '240',
source: newUrl,
embed_id: 'my-timeline'
});
});