1

在下面的下拉菜单中有由 etc 调用的 java 脚本函数。我onmouseover="openelement('menu2')" , onmouseover="openelement('menu3')"不明白这是什么'menu2&#39 , 'menu3'。有人可以解释一下吗?整个程序如下。

下拉菜单 CSS & JS

    <!--The CSS code.-->
    <style type="text/css" media="screen">

            #nav {
                margin: 0;
                padding: 0;
            }

            #nav li {
                list-style: none;
                float: left;
            }

            #nav li a {
                display: block;
                margin: 0 1px 0 0;
                padding: 4px 10px;
                width: 80px;
                background: lavender;
                color: black;
                text-align: center;
                text-decoration:none;
            }

            #nav li a:hover {
                background: grey;
            }

            #nav ul {
                position: absolute;
                visibility: hidden;
                margin: 0;
                padding: 0;
                border: 1px solid #ffffff
            }

            #nav  ul li a{
                    position: relative;
                    margin: 0;
                    padding: 5px 10px;
                    width: 80px;
                    text-align: left;
                    background: lavender;
                    color: #000000;
                }   

            #nav li ul li {
                clear: both;
            }
    </style>
    <!--The end of the CSS code.-->

    <!--The Javascript menu code.-->
    <script type="text/javascript">
        //variables' declaration

        var timeout = 500;
        var timer   = 0;
        var item    = 0;

        //function for opening of submenu elements
        function openelement(num)
        {    
            keepsubmenu()
            //checks whether there is an open submenu and makes it invisible
            if(item){ item.style.visibility = 'hidden';}
                //shows the chosen submenu element
                item = document.getElementById(num);
                item.style.visibility = 'visible';
        }

        // function for closing of submenu elements
        function closeelement()
        {
            //closes the open submenu elements and loads the timer with 500ms
            timer = window.setTimeout("if(item) item.style.visibility = 'hidden';",500);
        }

        //function for keeping the submenu loaded after the end of the 500 ms timer
        function keepsubmenu()
        {
            if(timer){
                window.clearTimeout(timer);
                timer = null;
            }
        }

        //hides the visualized menu after clicking outside of its area and expiring of the loaded timer
        document.onclick = closeelement;

    </script>
    <!--END of CSS code-->

</head>
<body>
    <!--HTML code for the menu -->
    <ul id="nav">
        <li><a href="http://www.sci.sjp.ac.lk/lms/file.php/233/dropDownJs.html#">Home</a>
        </li>
        <li><a href="http://www.sci.sjp.ac.lk/lms/file.php/233/dropDownJs.html#" onmouseover="openelement(&#39;menu2&#39;)" onmouseout="closeelement()">Tutorials</a>
            <ul id="menu2" onmouseover="keepsubmenu()" onmouseout="closeelement()" style="visibility: hidden; ">
                <li><a href="http://www.sci.sjp.ac.lk/lms/file.php/233/dropDownJs.html#">CSS</a></li>
                <li><a href="http://www.sci.sjp.ac.lk/lms/file.php/233/dropDownJs.html#">Flash</a></li>
            </ul>
        </li>
        <li><a href="http://www.sci.sjp.ac.lk/lms/file.php/233/dropDownJs.html#" onmouseover="openelement(&#39;menu3&#39;)" onmouseout="closeelement()">More</a>
            <ul id="menu3" onmouseover="keepsubmenu()" onmouseout="closeelement()">
               <li><a href="http://www.sci.sjp.ac.lk/lms/file.php/233/dropDownJs.html#">About Us</a></li>
                <li><a href="http://www.sci.sjp.ac.lk/lms/file.php/233/dropDownJs.html#">Contact Us</a></li> 
            </ul>
        </li>
    </ul>
    <div style="clear:both"></div>
    <!--the end of the HTML code for the menu -->

4

2 回答 2

3

39 是撇号的 ASCII 代码,&#39;是将 ASCII 字符插入 HTML 的语法

此语法主要用于按原样插入在 HTML / JavaScript 中有意义的字符(并防止跨站点脚本攻击) - 例如<>字符

在您的情况下,您应该替换 ' 在带有撇号 (') 的事件处理程序中发生,因此它在 Javascript 中的行为应如此

于 2013-05-18T01:58:33.593 回答
1

这只是一个撇号,可能来自错误的复制/粘贴。例如,如果您在 Word 中编写代码或从应用特殊格式的文本区域复制代码,就会发生这种情况。它应该是单引号,因为该函数需要一个字符串。

"openelement('menu3')"
于 2013-05-18T01:58:23.223 回答