我有以下代码结构。
<!-- first.tpl -->
<script>$(document).ready(function() {
objIns.loadNames = '{$names|json_encode}';
}
)</script>
{include file="second.tpl"}
<!-- end of first.tpl -->
<!-- second.tpl -->
some code here
<script>$(Document).ready(函数(){)
objIns.loadNames = '{$names|json_encode}';})
</script> 我的问题是,为什么我需要在objIns.loadNames文件中声明变量second.tpl,因为它已经加载在first.tpl文件中了?在second.tpl中不重新声明此变量的任何方式。我通过声明"var“关键字来尝试它,但是它不能工作。
发布于 2020-05-13 07:59:37
您可以通过将objIns作为全局变量来实现这一点。
<script>
var objIns;
$(document).ready(function() {
objIns.loadNames = '{$names|json_encode}';
</script>
{include file="second.tpl"}在函数中声明变量将使其作用域为局部变量,因此在函数之外声明变量,以便可以全局使用。
发布于 2020-05-13 08:07:16
定义您的objIns variable before $(document).ready function in first.tpl file as global
var objIns='';或
在javascript localstorage文件中使用first.tpl
在文档就绪函数中添加这些行
objIns.loadNames = '{$names|json_encode}';
localStorage.setItem("objIns",objIns.loadNames);在first.tpl文件中
访问存储在second.tpl中的应用
localStorage.getItem("objIns");有关js本地存储点击的更多信息
https://stackoverflow.com/questions/61769034
复制相似问题