gpt4 book ai didi

javascript - Uncaught ReferenceError :- variable is not defined(recursive calling)

转载 作者:行者123 更新时间:2023-11-28 04:23:34 26 4
gpt4 key购买 nike

我遇到了问题

Uncaught ReferenceError: fg_my_content is not defined
at flipGallery (flipgallery.min.js:53)
at asdfg (Albumfinal.php:420)
at onload (Albumfinal.php:21)

这是我的代码

<body onload="loadContent()">
.
.
.
.
<!-- DOM area must be loaded before executing scripts-->
<!-- /DOM area must be loaded before executing scripts-->
<script>
function loadContent(){
var fg_my_content1 = <?= json_encode($fg_my_content)?>;
var fg_my_content2 = <?= json_encode($fg_my_content2)?>;
var fg_my_content=fg_my_content1;
flipGallery();
fg_my_content=fg_my_content2;
flipGallery2();
}
</script>
<script type="text/javascript" src="flipgallery/flipgallery.min.js"></script><!-- has a function that uses variable fg_my_content -->
<script type="text/javascript" src="flipgallery2/flipgallery.min.js"></script><!-- has a function that uses variable fg_my_content -->
</body>

其中json_encode($fg_my_content)的值来自php

{
"user":{
"name": "something",
"age": 20,
"phonenumber": "321456987"
}
}

使用变量fg_my_content的函数看起来像

//flipgallery/flipgallery.min.js
function flipGallery(){
.
.
.
doSomethingWith(fg_my_content);
.
.
.
}
//flipgallery2/flipgallery.min.js
function flipGallery2(){
.
.
.
doSomethingWith(fg_my_content);
.
.
.
}

我的代码应该做的是加载html DOM,然后调用函数loadContent(),该函数应该声明变量fg_my_content,然后调用函数flipGallery() 。但由于某种原因,函数 flipGallery() 中 undefined variable fg_my_content。这是为什么?

为什么变量 fg_my_content 没有在 flipGallery() 中定义,即使我在调用函数之前声明它?

最佳答案

将 fg_my_content 定义为全局,如下所示:

<script>
var fg_my_content;
function loadContent(){
var fg_my_content1 = <?= json_encode($fg_my_content)?>;
var fg_my_content2 = <?= json_encode($fg_my_content2)?>;
fg_my_content=fg_my_content1;
flipGallery();
fg_my_content=fg_my_content2;
flipGallery2();
}
</script>

或者 将值 fg_my_content 传递给函数

关于javascript - Uncaught ReferenceError :- variable is not defined(recursive calling),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45253182/

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