我需要編寫一個函數,可以在普通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