我需要编写一个函数,可以在普通js中设置全局变量,使用jQuery函数更新其值,然后将通过jQuery函数更新的值分配给正常的javasciprt变量。使用Jquery更新全局JS变量并返回该值
下面是代码示例:
var foo; // global variable in plain js
var bar; // second global variable
jQuery(document).ready(function ($) {
// jquery function that updates the variable
foo = 'updated by jquery'
});
bar = foo;
console.log(bar); // console should read 'updated by jquery' but it says undefined
'foo'将加载DOM时,被更新,因为它是在的document.ready()函数。然而,'console.log'会在页面加载之前立即执行 – Bryan
我该如何完成这项工作?因为我需要传递jquery所做的更改并更新全局变量。 – n00b3