2017-05-27 75 views
-2
<!DOCTYPE html> 
<html> 
<style> 
body { 
    font-family: "Lato", sans-serif; 
} 

.sidenav { 
    height: 100%; 
    width: 0; 
    position: fixed; 
    z-index: 1; 
    top: 0; 
    left: 0; 
    background-color: #111; 
    overflow-x: hidden; 
    transition: 0.5s; 
    padding-top: 60px; 
    text-align:center; 
} 

.sidenav a { 
    padding: 8px 8px 8px 32px; 
    text-decoration: none; 
    font-size: 25px; 
    color: #818181; 
    display: block; 
    transition: 0.3s; 

} 

.sidenav a:hover{ 
    color: #f1f1f1; 
} 

.sidenav .closebtn { 
    position: absolute; 
    top: 0; 
    right: 25px; 
    font-size: 36px; 
    margin-left: 50px; 
} 

@media screen and (max-height: 450px) { 
    .sidenav {padding-top: 15px;} 
    .sidenav a {font-size: 18px;} 
} 
</style> 
<script> 
function ope() { 
    document.getElementById("mySidenav").style.width = "100%"; 
} 

function clo() { 
    document.getElementById("mySidenav").style.width = "0"; 
} 
</script> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> 
<body> 
<div id="mySidenav" class="sidenav"> 
    <a href="javascript:void(0)" class="closebtn" onclick="clo()">&times;</a> 
    <iframe width="420" height="345" src="https://www.youtube.com/embed/Uks8psEpmB4"> 
</iframe> 
</div> 
<div class="w3-container"> 


    <div class="w3-card-4 w3-dark-grey" style="width:50%"> 

    <div class="w3-container w3-center"> 
    <span style="cursor:pointer" onclick="ope()"> 
     <h3>Friend Request</h3> 
     <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
     <h5>John Doe</h5> 
     </span> 
    </div> 

    </div> 
</div> 



<div id="mySidenav" class="sidenav"> 
    <a href="javascript:void(0)" class="closebtn" onclick="clo()">&times;</a> 
    <iframe width="420" height="345" src="https://www.youtube.com/embed/1PPDoAKbaNA"> 
</iframe> 
</div> 
<div class="w3-container"> 


    <div class="w3-card-4 w3-dark-grey" style="width:50%"> 

    <div class="w3-container w3-center"> 
    <span style="cursor:pointer" onclick="ope()"> 
     <h3>Friend Request</h3> 
     <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
     <h5>John Doe</h5> 
     </span> 
    </div> 

    </div> 
</div> 

</body> 
</html> 


          
  
function ope() { 
 
     document.getElementById("mySidenav").style.width = "100%"; 
 
    } 
 

 
    function clo() { 
 
     document.getElementById("mySidenav").style.width = "0"; 
 
    }
body { 
 
     font-family: "Lato", sans-serif; 
 
    } 
 

 
    .sidenav { 
 
     height: 100%; 
 
     width: 0; 
 
     position: fixed; 
 
     z-index: 1; 
 
     top: 0; 
 
     left: 0; 
 
     background-color: #111; 
 
     overflow-x: hidden; 
 
     transition: 0.5s; 
 
     padding-top: 60px; 
 
     text-align:center; 
 
    } 
 

 
    .sidenav a { 
 
     padding: 8px 8px 8px 32px; 
 
     text-decoration: none; 
 
     font-size: 25px; 
 
     color: #818181; 
 
     display: block; 
 
     transition: 0.3s; 
 

 
    } 
 

 
    .sidenav a:hover{ 
 
     color: #f1f1f1; 
 
    } 
 

 
    .sidenav .closebtn { 
 
     position: absolute; 
 
     top: 0; 
 
     right: 25px; 
 
     font-size: 36px; 
 
     margin-left: 50px; 
 
    } 
 

 
    @media screen and (max-height: 450px) { 
 
     .sidenav {padding-top: 15px;} 
 
     .sidenav a {font-size: 18px;} 
 
    }
<!DOCTYPE html> 
 
    <html> 
 

 

 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> 
 
    <body> 
 
    <div id="mySidenav" class="sidenav"> 
 
     <a href="javascript:void(0)" class="closebtn" onclick="clo()">&times;</a> 
 
     <iframe width="420" height="345" src="https://www.youtube.com/embed/Uks8psEpmB4"> 
 
    </iframe> 
 
    </div> 
 
    <div class="w3-container"> 
 

 

 
     <div class="w3-card-4 w3-dark-grey" style="width:50%"> 
 

 
     <div class="w3-container w3-center"> 
 
     <span style="cursor:pointer" onclick="ope()"> 
 
      <h3>Friend Request</h3> 
 
      <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
 
      <h5>John Doe</h5> 
 
      </span> 
 
     </div> 
 

 
     </div> 
 
    </div> 
 

 

 

 
    <div id="mySidenav" class="sidenav"> 
 
     <a href="javascript:void(0)" class="closebtn" onclick="clo()">&times;</a> 
 
     <iframe width="420" height="345" src="https://www.youtube.com/embed/1PPDoAKbaNA"> 
 
    </iframe> 
 
    </div> 
 
    <div class="w3-container"> 
 

 

 
     <div class="w3-card-4 w3-dark-grey" style="width:50%"> 
 

 
     <div class="w3-container w3-center"> 
 
     <span style="cursor:pointer" onclick="ope()"> 
 
      <h3>Friend Request</h3> 
 
      <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
 
      <h5>John Doe</h5> 
 
      </span> 
 
     </div> 
 

 
     </div> 
 
    </div> 
 

 
</body> 
 
</html> 
 

每次我寫iframe的SRC不同的鏈接重定向到總是前一個導致了很多問題,我sdhcfgfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff如何在2個不同的iframe中添加2個不同的鏈接在相同的身體?

回答

1

所以有幾個問題與您的代碼:

首先,在HTML ID名稱是唯一的,並且您有兩個具有相同ID的元素,即mySidenav。這是你的代碼無法工作的主要原因,因爲它不知道你的目標是哪一個。

要解決你的代碼執行以下操作:

<!-- First video --> 
<div id="first" class="sidenav"> 
    <a href="javascript:void(0)" class="closebtn" onclick="clo.call(this)" data-cls="first">&times;</a> 
    <iframe width="420" height="345" src="https://www.youtube.com/embed/Uks8psEpmB4"></iframe> 
</div> 

<div class="w3-card-4 w3-dark-grey" style="width:50%"> 
    <div class="w3-container w3-center"> 
    <span style="cursor:pointer" onclick="ope.call(this)" data-id="first"> 
     <h3>Friend Request</h3> 
     <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
     <h5>John Doe</h5> 
    </span> 
    </div> 
</div> 



<!-- Second video --> 
<div id="second" class="sidenav"> 
    <a href="javascript:void(0)" class="closebtn" onclick="clo.call(this)" data-cls="second">&times;</a> 
    <iframe width="420" height="345" src="https://www.youtube.com/embed/1PPDoAKbaNA"></iframe> 
</div> 

<div class="w3-card-4 w3-dark-grey" style="width:50%"> 
    <div class="w3-container w3-center"> 
    <span style="cursor:pointer" onclick="ope.call(this)" data-id="second" > 
     <h3>Friend Request</h3> 
     <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
     <h5>John Doe</h5> 
     </span> 
    </div> 
    </div> 

至於的JavaScript,更改爲:

function ope() { 
    let vidPos = this.getAttribute("data-id"); 
    document.querySelector("#"+vidPos).style.width = "100%"; 
} 

function clo() { 
    let vidPos = this.getAttribute("data-cls"); 
    document.querySelector("#"+vidPos).style.width = "0%"; 
} 

你應該注意幾件事情: 1)我改變ope()ope.call(this)到將窗口對象的上下文更改爲元素。 2)我添加了自定義數據屬性,以幫助區分和定位不同的元素並將它們鏈接到不同的視頻。

您的代碼應該按照您在下面的演示中所預期的那樣工作。

function ope() { 
 
     let vidPos = this.getAttribute("data-id"); 
 
     document.querySelector("#"+vidPos).style.width = "100%"; 
 
} 
 

 
function clo() { 
 
     let vidPos = this.getAttribute("data-cls"); 
 
     document.querySelector("#"+vidPos).style.width = "0%"; 
 
}
body { 
 
     font-family: "Lato", sans-serif; 
 
    } 
 

 
    .sidenav { 
 
     height: 100%; 
 
     width: 0; 
 
     position: fixed; 
 
     z-index: 1; 
 
     top: 0; 
 
     left: 0; 
 
     background-color: #111; 
 
     overflow-x: hidden; 
 
     transition: 0.5s; 
 
     padding-top: 60px; 
 
     text-align:center; 
 
    } 
 

 
    .sidenav a { 
 
     padding: 8px 8px 8px 32px; 
 
     text-decoration: none; 
 
     font-size: 25px; 
 
     color: #818181; 
 
     display: block; 
 
     transition: 0.3s; 
 

 
    } 
 

 
    .sidenav a:hover{ 
 
     color: #f1f1f1; 
 
    } 
 

 
    .sidenav .closebtn { 
 
     position: absolute; 
 
     top: 0; 
 
     right: 25px; 
 
     font-size: 36px; 
 
     margin-left: 50px; 
 
    } 
 

 
    @media screen and (max-height: 450px) { 
 
     .sidenav {padding-top: 15px;} 
 
     .sidenav a {font-size: 18px;} 
 
    }
<!DOCTYPE html> 
 
    <html> 
 

 

 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> 
 
    <body> 
 
    <div id="first" class="sidenav"> 
 
     <a href="javascript:void(0)" class="closebtn" onclick="clo.call(this)" data-cls="first">&times;</a> 
 
     <iframe width="420" height="345" src="https://www.youtube.com/embed/Uks8psEpmB4"> 
 
    </iframe> 
 
    </div> 
 
    <div class="w3-container"> 
 

 

 
     <div class="w3-card-4 w3-dark-grey" style="width:50%"> 
 

 
     <div class="w3-container w3-center"> 
 
     <span style="cursor:pointer" onclick="ope.call(this)" data-id="first"> 
 
      <h3>Friend Request</h3> 
 
      <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
 
      <h5>John Doe</h5> 
 
      </span> 
 
     </div> 
 

 
     </div> 
 
    </div> 
 

 

 

 
    <div id="second" class="sidenav"> 
 
     <a href="javascript:void(0)" class="closebtn" onclick="clo.call(this)" data-cls="second">&times;</a> 
 
     <iframe width="420" height="345" src="https://www.youtube.com/embed/1PPDoAKbaNA"> 
 
    </iframe> 
 
    </div> 
 
    <div class="w3-container"> 
 

 

 
     <div class="w3-card-4 w3-dark-grey" style="width:50%"> 
 

 
     <div class="w3-container w3-center"> 
 
     <span style="cursor:pointer" onclick="ope.call(this)" data-id="second" > 
 
      <h3>Friend Request</h3> 
 
      <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
 
      <h5>John Doe</h5> 
 
      </span> 
 
     </div> 
 

 
     </div> 
 
    </div> 
 

 
</body> 
 
</html>

0

這是一個正確的格式,如果您有任何疑問,請聯絡我Easily Learn Contact Form。請別說你的名字,你的電子郵箱

function ope() { 
 
    document.getElementById("mySidenav").style.width = "100%"; 
 
} 
 

 
function clo() { 
 
    document.getElementById("mySidenav").style.width = "0"; 
 
}
body { 
 
    font-family: "Lato", sans-serif; 
 
} 
 

 
.sidenav { 
 
    height: 100%; 
 
    width: 0; 
 
    position: fixed; 
 
    z-index: 1; 
 
    top: 0; 
 
    left: 0; 
 
    background-color: #111; 
 
    overflow-x: hidden; 
 
    transition: 0.5s; 
 
    padding-top: 60px; 
 
    text-align:center; 
 
} 
 

 
.sidenav a { 
 
    padding: 8px 8px 8px 32px; 
 
    text-decoration: none; 
 
    font-size: 25px; 
 
    color: #818181; 
 
    display: block; 
 
    transition: 0.3s; 
 

 
} 
 

 
.sidenav a:hover{ 
 
    color: #f1f1f1; 
 
} 
 

 
.sidenav .closebtn { 
 
    position: absolute; 
 
    top: 0; 
 
    right: 25px; 
 
    font-size: 36px; 
 
    margin-left: 50px; 
 
} 
 

 
@media screen and (max-height: 450px) { 
 
    .sidenav {padding-top: 15px;} 
 
    .sidenav a {font-size: 18px;} 
 
}
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 

 
<div id="mySidenav" class="sidenav"> 
 
    <a href="javascript:void(0)" class="closebtn" onclick="clo()">&times;</a> 
 
    <iframe width="420" height="345" src="https://www.youtube.com/embed/Uks8psEpmB4"> 
 
</iframe> 
 
</div> 
 
<!-- First Iframe 1 --> 
 
<div class="w3-container"> 
 
    <div class="w3-card-4 w3-dark-grey" style="width:50%"> 
 
    <div class="w3-container w3-center"> 
 
    <span style="cursor:pointer" onclick="ope()"> 
 
     <h3>Friend Request</h3> 
 
     <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
 
     <h5>John Doe</h5> 
 
     </span> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<!-- First Iframe 2 --> 
 
<div class="w3-container"> 
 
    <div class="w3-card-4 w3-dark-grey" style="width:50%"> 
 
    <div class="w3-container w3-center"> 
 
    <span style="cursor:pointer" onclick="ope()"> 
 
     <h3>Friend Request</h3> 
 
     <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
 
     <h5>John Doe</h5> 
 
     </span> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<br /> 
 

 
<!-- Second Iframe 1 --> 
 
<div class="w3-container"> 
 
    <div class="w3-card-4 w3-dark-grey" style="width:50%"> 
 
    <div class="w3-container w3-center"> 
 
    <span style="cursor:pointer" onclick="ope()"> 
 
     <h3>Friend Request</h3> 
 
     <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
 
     <h5>John Doe</h5> 
 
     </span> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<!-- Second Iframe 2 --> 
 
<div class="w3-container"> 
 
    <div class="w3-card-4 w3-dark-grey" style="width:50%"> 
 
    <div class="w3-container w3-center"> 
 
    <span style="cursor:pointer" onclick="ope()"> 
 
     <h3>Friend Request</h3> 
 
     <img src="img_avatar3.png" alt="Avatar" style="width:80%"> 
 
     <h5>John Doe</h5> 
 
     </span> 
 
    </div> 
 
    </div> 
 
</div>

相關問題