全局变量的jquery值在执行时不会出现

时间:2014-08-12 21:02:52

标签: javascript jquery

以下代码运行,但是,我的问题是,对于变量' cc',我无法获得该值。当页面加载时,我创建了元素'输入'将此分配到全局变量&globalVar',我的问题是无法获得全局变量的值&globalVar',请参阅代码,您可以有一些想法。有帮助吗?它会更有帮助。

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>eq demo</title>
<style>
div, .sectiontitle {
width: 60px;
height: 60px;
margin: 10px;
float: left;
border: 2px solid blue;
}
.blue {
background: yellow;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<ul class="mainnav" id="mainnav1"><li><a href="/Home.aspx"><span>Home</span></a></li><li><a href="/Test-Orders/Home.aspx"><span>Test orders</span></a></li><li><a href="/Access-Arrangements/Home.aspx"><span>Access arrangements</span></a></li><li><a href="/Pupils/Home.aspx"><span>Pupil registration</span></a></li><li><a href="/Teacher-Assessment/Home.aspx" class="active"><span>Teacher assessment</span></a></li><li><a href="/Pupil-results/Home.aspx?stage=2"><span>Pupil results</span></a></li><li><a href="/Phonics/Home.aspx"><span>Phonics screening check</span></a></li></ul></div>
<div></div>
<div class="sectiontitle clearfix"><p class="breadcrumb"><a href="/Home.aspx">NCA tools</a><a href="Home.aspx">Teacher assessment</a></p></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>


<script>
            window.globalVar = "";

            globalVar = document.createElement("input");
            globalVar.id = "li2";
            globalVar.type = "text";

            $(document).ready(function () {

                $("#mainnav1 li a").click(function (e) {

                    globalVar.value = $(this).find('span').text().toLowerCase();
                    //alert(globalVar); //here, the value is coming
                    return globalVar;
                });

                var headerHighlightMap = {
                    "nca tools": "home",
                    "test orders": "test orders",
                    "access arrangements": "access arrangements",
                    "pupil registration": "pupil registration",
                    "teacher assessment": "teacher assessment",
                    "pupil results": "pupil results",
                    "phonics screening check": "phonics screening check"
                };

                var currentHeaderText = $.trim($(".sectiontitle h1").html()).toLowerCase();
                var currentHeaderTextsel = $.trim($(".sectiontitle p.breadcrumb a").eq(1).html()).toLowerCase();
                $.each($("#mainnav1 li"), function (index) {
                    var cc = globalVar.value;
                    alert(cc);
                    var currentElemSpanObj = $($(this).find("span"));
                    var headerInFocus = $.trim(currentElemSpanObj.html()).toLowerCase();
                    //alert(headerInFocus);
                    if (headerHighlightMap[currentHeaderText] == headerInFocus) {
                        currentElemSpanObj.addClass("blue");
                    }
                    else if (headerHighlightMap[currentHeaderTextsel] == headerInFocus) {
                        currentElemSpanObj.addClass("blue");
                    }
                    else if (headerHighlightMap[globalVar.value] == headerInFocus) {
                        currentElemSpanObj.addClass("blue");
                    }

                    else {
                        currentElemSpanObj.removeClass("blue");
                    }
                });
            });
            </script>
</body>
</html>

0 个答案:

没有答案