Now, if the callback function passed to sendRequest is executed in a context where that code cannot be executed, as may well be the case with browser extensions, you might have to pass a reference to the object instead:$(document).ready(function() { var myObj = $("#id"); chrome.extension.sendRequest({get: "height"}, function(response) { height = response.value; myObj.css("height", height+"px"); });});但是如果可以的话,我会选择第一个版本.But I'd go with the first version if that works.应用此功能后,调查是否仍然需要保持height全局变量...When you've applied this, investigate whether you still need to keep the height variable global... 这篇关于JavaScript全局变量无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 10:20