我有以下代码结构。
<!-- 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"}在函数中声明变量将使其作用域为局部变量,因此在函数之外声明变量,以便可以全局使用。
https://stackoverflow.com/questions/61769034
复制相似问题