Javascript(0)以及HTML5中的外部Javascript

Javascript(0) along with external Javascript in HTML5

本文关键字:Javascript 外部 HTML5 以及      更新时间:2023-09-26

我想知道是否有人可以帮助我。我坚持这段代码,我的脚本切换按钮,当我点击它,当我点击切换窗口外,它会消失,经过2小时的搜索和测试出来。我还是想不明白。目前的脚本甚至不工作,切换菜单甚至没有出来。当运行外部jscript时,如topmenusscript .js它说引用错误,函数未定义ReferenceError: DropdownFunction is not defined

HTML5

<%@ Page Language="C#" AutoEventWireup="true"  CodeBehind="WebForm1.aspx.cs" Inherits="Tutorial_Site.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
    #form1 {
        width: 1021px;
    }
    .TopPanel {}
    body,html{
        height:100%;
        width:100%;
        overflow:auto;
        margin:0;
        padding:0;
    }
    .TopBannerImage {
        background:url(../Images/topPanel_3croped.png) center;
        height:315px;
        min-width:100%;
        background-repeat:no-repeat;
    }
</style>
<link href="CssFiles/MainPage.css" rel="stylesheet" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body style="background-color:#3F3939" class ="Test">
<div id="TopPanelMenuDiv" style="background-color:black;">
    <ul id="ul_Top">
        <li><a class ="active" href="#home">Home</a></li>
        <li style="float:right !important"><a href="#news">News</a></li>
        <li class ="dropdown">
<a href="javascript:void(0)" class="dropbtn" onclick="DropdownFunction()">Dropdown</a>
<div class="dropdown-content" id="MenuDropdown">
  <a href="#">Link 1</a>
  <a href="#">Link 2</a>
  <a href="#">Link 3</a>
</div>
        </li>
    </ul>
</div>
<div class="TopBannerImage">
</div>
<!--All Site Scripts -->
<!-- <script>
    function DropdownFunction() {
        document.getElementById("MenuDropdown").classList.toggle("show");
    }
    window.onclick = function (e) {
        if (!e.target.matches('.dropbtn')) {
            var dropdowns = document.getElementsByClassName("dropdown-content");
            for (var d = 0; d < dropdowns.length; d++) {
                var openDropdown = dropdowns[d];
                if (openDropdown.classList.contains('show')) {
                    openDropdown.classList.remove('show');
                }
            }
        }
    }
</script> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="/JavaScript/jquery-3.1.1.min.js" ></script>
<script type ="text/javascript" src="/JavaScript/TopMenuScript.js" ></script>
<script src="Scripts/jquery-1.10.2.min.js"></script>
</body>

Javascript

 <script>
    function DropdownFunction() {
        document.getElementById("MenuDropdown").classList.toggle("show");
    }
    window.onclick = function (e) {
        if (!e.target.matches('.dropbtn')) {
            var dropdowns = document.getElementsByClassName("dropdown-content");
            for (var d = 0; d < dropdowns.length; d++) {
                var openDropdown = dropdowns[d];
                if (openDropdown.classList.contains('show')) {
                    openDropdown.classList.remove('show');
                }
            }
        }
    }
</script> 

使用相同脚本的外部脚本链接

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="/JavaScript/jquery-3.1.1.min.js" ></script>
<script type ="text/javascript" src="/JavaScript/TopMenuScript.js" ></script>
<script src="Scripts/jquery-1.10.2.min.js"></script>
css

    .TopPanel
{
    border-style:ridge;
    border-width:2px;
    border-color:black;
    margin:auto;
    padding: 0px;
    text-align: center;
    background-size:cover;
    background-image url(../Images/background.jpg);
    background-repeat: no-repeat;
}
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: black;
    height: 49px;
}
li{
    float:left;
}
li a, .dropbin {
    display:inline-block;
    color:white;
    text-align:center;
    padding:14px 16px;
    text-decoration:none;
}
li a, .dropdown:hover .dropbtn {
    background-color:black;
}
li.dropdown {
    display:inline-block;
}
.dropdown-content {
    display:none;
    position: absolute;
    background-color: #f9f9f9;
    min-width:160px;
    box-shadow : 0px 8px 16px 0px RGBA(0,0,0,0.2)
}
.dropdown-content a {
    color:black;
    padding:12px 16px;
    text-decoration: none;
    display:block;
}
.dropdown-content .dropdown-content {
    display:block;
}
body{margin:0;padding:0;}
.show {display:block;}

我已经为你清理并修复了一切。在visual studio 2015中运行它,它工作得很好,也修复了你的UI。问题的主要原因,语法错误和拼写错误。你要确保javascript中没有语法错误。一个错误的语法就会导致。我相信我们在你的页面上看到了没有看到的错误。在编码中,它是区分大小写的-继续编程,不管你失败了多少次,因为学习是没有尽头的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Tutorial_Site.WebForm1" %>

HTML5代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
    #form1 {
        width: 1021px;
    }
    .TopPanel {}
    body,html{
        height:100%;
        width:100%;
        overflow:auto;
        margin:0;
        padding:0;
    }
    .TopBannerImage {
        background:url(../Images/topPanel_3croped.png) center;
        height:315px;
        min-width:100%;
        background-repeat:no-repeat;
    }
</style>
<link href="CssFiles/MainPage.css" rel="stylesheet" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body style="background-color:#3F3939" class ="Test">
<div id="TopPanelMenuDiv" style="background-color:black;">
    <ul id="ul_Top">
        <li><a class ="active" href="#home">Home</a></li>
        <li style="float:right !important"><a href="#news">News</a></li>
        <li class ="dropdown">
<a href="javascript:void(0)" class="dropbtn" onclick="DropdownFunction()">Dropdown</a>
<div class="dropdown-content" id="MenuDropdown">
  <a href="#">Link 1</a>
  <a href="#">Link 2</a>
  <a href="#">Link 3</a>
</div>
        </li>
    </ul>
</div>
<div class="TopBannerImage">
</div>
<!--All Site Scripts -->
<!-- <script>
    function DropdownFunction() {
        document.getElementById("MenuDropdown").classList.toggle("show");
    }
    window.onclick = function (e) {
        if (!e.target.matches('.dropbtn')) {
            var dropdowns = document.getElementsByClassName("dropdown-content");
            for (var d = 0; d < dropdowns.length; d++) {
                var openDropdown = dropdowns[d];
                if (openDropdown.classList.contains('show')) {
                    openDropdown.classList.remove('show');
                }
            }
        }
    }
</script> -->
<script src="/JavaScript/jquery-3.1.1.min.js" ></script>
<script type ="text/javascript" src="JavaScript/TopMenuScript.js" ></script>
</body>
</html>

JavaScript代码

function DropdownFunction() {
    document.getElementById("MenuDropdown").classList.toggle("show");
}
window.onclick = function (e) {
    if (!e.target.matches('.dropbtn')) {
        var dropdowns = document.getElementsByClassName("dropdown-content");
        for (var d = 0; d < dropdowns.length; d++) {
            var openDropdown = dropdowns[d];
            if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
            }
        }
    }
}

css代码

    .TopPanel
    {
    border-style:ridge;
    border-width:2px;
    border-color:black;
    margin:auto;
    padding: 0px;
    text-align: center;
    background-size:cover;
    background-image url(../Images/background.jpg);
    background-repeat: no-repeat;
    }
    ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
    height: 49px;
    }
    li{
    float:left;
}
    li a, .dropbin {
    display:inline-block;
    color:white;
    text-align:center;
    padding:14px 16px;
    text-decoration:none;
}
    li a, .dropdown:hover .dropbtn {
    background-color:black;
}
    li.dropdown {
    display:inline-block;
}
    .dropdown-content {
    display:none;
    position: absolute;
    background-color: #f9f9f9;
    min-width:160px;
    box-shadow : 0px 8px 16px 0px rgba(0,0,0,0.2)
}
    .dropdown-content a {
    color:white;
    padding:12px 16px;
    text-decoration: none;
    display:block;
    text-align:left;
}
    .dropdown-content .dropdown-content {
    display:block;
}
    body{margin:0;padding:0;}
    .dropdown-content a:hover {background-color:#f1f1f1}
    .show {display:block;}

    #BannerImage {
    background-image: url('/Images/topPanel_3croped.png');
    width:100%;
    height:100%;
    background-repeat:no-repeat;
}
    #my-div{
    margin-left:auto;
    margin-right:auto;
}