日期:2014-05-16  浏览次数:20320 次

javascript全局变量的赋值问题
$(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赋值,为什么呢?该怎么改?

------解决方案--------------------
$(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>");
    });