繁体   English   中英

使用document.getElementById引用多个(不同)Div ID

[英]Reference multiple (different) Div Id's using document.getElementById

我在SharePoint中使用脚本编辑器Web部件来创建全屏覆盖导航功能。

我从https://www.w3schools.com/howto/howto_js_fullscreen_overlay.asp获得了代码。

我对其进行了一些修改,因为我想单击多个使用此功能的菜单。 我尝试上传图片,但没有10个声誉点:/以下是文字表示。 我有一个教义菜单链接和一个TTP菜单链接。

  • 教义
  • TTP

他们工作; 但是,无论我单击哪一个,它们都显示与TTP相关的链接。 我要单击“教义”并查看“教义”链接,然后单击“ TTP”以查看TTP链接。 两个单独的导航菜单分别执行全屏覆盖功能。

我知道这个主题已被殴打致死,但我找不到满足我要求的任何东西。 下面是我如何引用getElementById

注意:我用虚拟链接替换了实际链接。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
    font-family: 'Lato', sans-serif;
}

.overlay {
    height: 0%;
    width: 100%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(28,65,104, 0.9);
    overflow-y: hidden;
    transition: 0.5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #eccb13;
    display: block;
    transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
    color: #800000;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay {overflow-y: auto;}
  .overlay a {font-size: 20px}
  .overlay .closebtn {
    font-size: 40px;
    top: 15px;
    right: 35px;
  }
}
</style>
</head>
<body>

<div id="doctrineNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">Car</a>
    <a href="#">Bicycle</a>
    <a href="#">Boat</a>
    <a href="#">Airplane</a>
  </div>

</div>

<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9758; Doctrine</span>

<p></p>

<div id="ttpNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">Apple</a>
    <a href="#">Orange</a>
    <a href="#">Dog</a>
    <a href="#">Cat</a>
  </div>

</div>

<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9758; TTP</span>

<script>
function openNav() {
   document.getElementById("doctrineNav").style.height = "100%";
}

function closeNav() {
   document.getElementById("doctrineNav").style.height = "0%";
}

function openNav() {
   document.getElementById("ttpNav").style.height = "100%";
}

function closeNav() {
   document.getElementById("ttpNav").style.height = "0%";
}

</script>

</body>
</html>

如果我像下面那样更改脚本,那么无论我单击哪个脚本,它们都将相互重叠。 换句话说,我可以看到TTP链接,也可以看到其背后的教义链接。

<script>
function openNav() {
   document.getElementById("doctrineNav").style.height = "100%";
   document.getElementById("ttpNav").style.height = "100%";
}

function closeNav() {
   document.getElementById("doctrineNav").style.height = "0%";
   document.getElementById("ttpNav").style.height = "0%";
}

</script>

问题在于,您已经为两组函数命名相同。 当您第二次声明openNav显示TTP时,它将替换第一个实例。 一个简单的解决方案是只声明两个独立的函数集,例如:

function openDoctrine() {
   document.getElementById("doctrineNav").style.height = "100%";
}

function closeDoctrine() {
   document.getElementById("doctrineNav").style.height = "0%";
}

function openTTP() {
   document.getElementById("ttpNav").style.height = "100%";
}

function closeTTP() {
   document.getElementById("ttpNav").style.height = "0%";
}

并酌情引用它们,例如:

<div id="doctrineNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  ...
</div>
...
<span style="font-size:30px;cursor:pointer" onclick="openDoctrine()">&#9758; Doctrine</span>

简而言之,您无法在JavaScript中重用名称(引擎应如何知道您要引用的功能?),因此请针对不同的任务使用更特定的名称。

@Ken Bellows有一个很好的答案。 我发现有用的另一件事是向您要选择的元素添加一个额外的类。 该类应该没有额外的CSS,其唯一目的是允许您用一行选择多个div。 然后, 使用此处的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM