javascript全局变量的赋值问题

acgCode 2012-12-27 08:18:35
$(Windows).ready(function () {
text = "失败";

$(".LevelChoice>a>img").click(function () {
text = "成功";
});
});
这个是一个js文件中的代码

<div class="LevelChoice" id="Easy"><a href="../GamePage/GamePage.html" id="easy"><img src="../../images/button/easy.png" alt="Easy" /></a></div>
<div class="LevelChoice" id="Normal"><a href="../GamePage/GamePage.html" id="normal"><img src="../../images/button/normal.png" alt="Normal" /></a></div>
<div class="LevelChoice" id="Difficult"><a href="../GamePage/GamePage.html" id="difficult"><img src="../../images/button/difficult.png" alt="Difficult" /></a></div>
这个是调用click方法的html代码

$(document).ready(function () {
$("#Questions").append("<h1>" + text + "</h1>");
});
这个是要调用那个更改后的全局变量的html页的js代码

三段代码放在三个文件夹内,声明全局变量的第一个JS文件代码在下面的两段代码所属的html文件中已经引用了,但是还是无法通过函数给全局变量text赋值,为什么呢?该怎么改?
...全文
343 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
acgCode 2012-12-29
  • 打赏
  • 举报
回复
唉~~~~,貌似想要使用全局变量有点不靠谱了,还是用URL吧~~~~
zyjj1688 2012-12-27
  • 打赏
  • 举报
回复
看得仔细,学习。
  • 打赏
  • 举报
回复
$(Windows) 这个错了吧,是window text更改后是要click后才会变, $(document).ready(function () { $("#Questions").append("<h1>" + text + "</h1>"); }); 这个是dom准备好后就就执行了,如果没有click过text当然没变了。。你直接放代码到click事件中不就行了?
    $(".LevelChoice>a>img").click(function () {
        text = "成功";
    $("#Questions").append("<h1>" + text + "</h1>");
    });

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧