gpt4 book ai didi

javascript - 从ajax页面获取js变量

转载 作者:行者123 更新时间:2023-11-28 16:15:26 24 4
gpt4 key购买 nike

我的问题是这样的,如果我有一个页面说index.HTML,里面有一些脚本,比如......

<script type="text/JavaScript">
$(document).ready(function() {
Var buttonBox = {};
})
</script>

显然还需要更多,我试图让这个变得简单。然后我使用 ajax 从数据库检索一些数据并填充 div 的内容,但在我的返回页面中我有另一个脚本标记,例如...

<script type="text/JavaScript">
$(function() {
buttonBox.start = "some variable or string";
})
</script>

以及 HTML 内容。为什么buttonBox.start在主index.HTML页面中不可用?有办法让它可用吗?将我的服务器页面的输出格式化为一个巨大的 json 对象,然后解析它以设置每个需要的变量以及 HTML 内容是实现此目的的最佳/唯一方法吗?

感谢您的帮助,如果您需要更多信息,我很乐意提供,为了方便起见,我只是将其缩小。

最佳答案

您可以将buttonBox添加到窗口并使其全局:

$(document).ready(function() {
window.buttonBox = {};
});

$(function() {
window.buttonBox.start = "some variable or string";
});

关于javascript - 从ajax页面获取js变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11625540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com