2017-06-02 14 views
0

これを参照してくださいfiddle。これは私の腕時計の予告編ボタンです。最初のボタンは完璧に機能しています。しかし、2番目の時計の予告編ボタンは機能していません。同じCSSとJSの2つのボタンが機能しません。

問題は何ですか?

ここ

が私のコードです:

HTML:

<button id="watchbutton">Watch Trailer &#9658</button> 
<br> 
<button id="watchbutton">Watch Trailer &#9658</button> 

<div id="close"> 
<div id="trailerdivbox"> 
<div class="videoWrapper"> 
    <iframe id="video" class="trailervideo" width="560" height="315" data- 
src="https://www.youtube.com/embed/TDwJDRbSYbw" src="about:blank" frameborder="0" allowfullscreen></iframe> 
</div> 
</div> 
</div> 

CSS:

/* Watch Trailer Button CSS */ 

#watchbutton { 
background-color: #f2f2f2; 
color: red; 
font-weight: 600; 
border: none; 
/* This one removes the border of button */ 
padding: 10px 12px; 
} 

#watchbutton:hover { 
background-color: #e2e2e2; 
cursor: pointer; 
} 

#trailerdivbox { 
display: none; 
width: 100%; 
height: 100%; 
position: fixed; 
top:0%; 
overflow: auto; /* Enable Scrolling */ 
background-color: rgb(0, 0, 0); 
/* Fallback color */ 
background-color: rgba(0, 0, 0, 0.4); 
/* Black w/ opacity */ 
} 

.videoWrapper { 
position: relative; 
padding-bottom: 56.25%; 
/* 16:9 */ 
padding-top: 25px; 
height: 0; 
} 

.videoWrapper iframe { 
position: absolute; 
max-width: 560px; 
max-height: 315px; 
width: 95%; 
height: 95%; 
left: 0; 
right: 0; 
margin: auto; 
} 

Javascriptを

<script> 
// Get the modal 
var modal = document.getElementById('trailerdivbox'); 

// Get the button that opens the modal 
var btn = document.getElementById("watchbutton"); 

function playVideo() { 
var video = document.getElementById('video'); 
var src = video.dataset.src; 

video.src = src + '?autoplay=1'; 
} 

function resetVideo() { 
var video = document.getElementById('video'); 
var src = video.src.replace('?autoplay=1', ''); 

video.src = ''; 
video.src = src; 
} 

// When the user clicks the button, open the modal 
btn.onclick = function() { 
modal.style.display = "block"; 
playVideo(); 
} 

var trailerbox = document.getElementById("close"); 

trailerbox.onclick = function() { 
modal.style.display = "none"; 
resetVideo(); 
} 

// When the user clicks anywhere outside of the modal, close it 
window.onclick = function(event) { 
if (event.target == modal) { 
modal.style.display = "none"; 
resetVideo(); 
} 
} 

</script> 
+2

を、しかし、両方のボタンが同じIDを持っています。これは直ちに解決する必要がある**問題です。IDは、文書内の他のすべての要素との関係で完全に一意であることを意味します。つまり、要素が他の要素とIDを共有する必要はありません**。 – Frits

+0

Frits、prasad&othersが言及しているdouble idで問題を修正した後も、プッシュするボタンに応じて異なるビデオを再生するように 'playVideo'に指示する必要があります。方法については、[** my answer below **](https://stackoverflow.com/questions/44330728/2-buttons-with-same-css-and-js-are-not-working/44331691#44331691)を参照してください。それをする。 –

答えて

2

classname代わりのid .Because IDで試してみてくださいは.Thenボタンのdata-srcに映像の

宣言srcを更新querySelectorAll()

でセレクタを使用.FOR全体htmlのためのユニークであります引数をplayvideo(this.dataset.src)として渡します。

// Get the modal 
 
var modal = document.getElementById('trailerdivbox'); 
 

 
// Get the button that opens the modal 
 
var btn = document.querySelectorAll('.watchbutton') 
 

 
function playVideo(src) { 
 
    var video = document.getElementById('video'); 
 
    video.src = 'https://www.youtube.com/embed/'+src + '?autoplay=1'; //add with iframe 
 
} 
 

 
function resetVideo() { 
 
    var video = document.getElementById('video'); 
 
    var src = video.src.replace('?autoplay=1', ''); 
 

 
    video.src = ''; 
 
    video.src = src; 
 
} 
 

 
// When the user clicks the button, open the modal 
 
btn.forEach(function(a){ 
 
a.onclick = function() { 
 
    modal.style.display = "block"; 
 
    playVideo(this.dataset.src); // pass the src 
 
} 
 
}) 
 

 
var trailerbox = document.getElementById("close"); 
 

 
trailerbox.onclick = function() { 
 
    modal.style.display = "none"; 
 
    resetVideo(); 
 
} 
 

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target == modal) { 
 
    modal.style.display = "none"; 
 
    resetVideo(); 
 
    } 
 
}
/* Watch Trailer Button CSS */ 
 

 
#watchbutton { 
 
    background-color: #f2f2f2; 
 
    color: red; 
 
    font-weight: 600; 
 
    border: none; 
 
    /* This one removes the border of button */ 
 
    padding: 10px 12px; 
 
} 
 

 
#watchbutton:hover { 
 
    background-color: #e2e2e2; 
 
    cursor: pointer; 
 
} 
 

 
#trailerdivbox { 
 
    display: none; 
 
    width: 100%; 
 
    height: 100%; 
 
    position: fixed; 
 
    overflow: auto; 
 
    /* Enable Scrolling */ 
 
    background-color: rgb(0, 0, 0); 
 
    /* Fallback color */ 
 
    background-color: rgba(0, 0, 0, 0.4); 
 
    /* Black w/ opacity */ 
 
} 
 

 
.videoWrapper { 
 
    position: relative; 
 
    padding-bottom: 56.25%; 
 
    /* 16:9 */ 
 
    padding-top: 25px; 
 
    height: 0; 
 
} 
 

 
.videoWrapper iframe { 
 
    position: absolute; 
 
    max-width: 560px; 
 
    max-height: 315px; 
 
    width: 95%; 
 
    height: 95%; 
 
    left: 0; 
 
    right: 0; 
 
    margin: auto; 
 
}
<button id="watchbutton" class="watchbutton" data-src="TDwJDRbSYbw">Watch Trailer &#9658</button> 
 
<br> 
 
<button id="watchbutton" class="watchbutton" data-src="TDwJDRbSYbr">Watch Trailer &#9658</button> 
 

 

 

 
<div id="close"> 
 
    <div id="trailerdivbox"> 
 
    <div class="videoWrapper"> 
 
     <iframe id="video" class="trailervideo" width="560" height="315" data-src="https://www.youtube.com/embed/TDwJDRbSYbw" src="about:blank" frameborder="0" allowfullscreen></iframe> 
 
    </div> 
 
    </div> 
 
</div> 
 
<br>

+0

Worked。しかしもう一つ忘れました。このフィドルを見てください:https://jsfiddle.net/kz0xxe22/8/ 両方のボタンが同じビデオを再生しています、それを修正する方法を教えてくださいできますか?そのボックスには多くのIDがありますが、どちらを修正すればよいですか? –

+0

@PunitMishra私の更新された回答を参照してください。私はあなたが1つを必要とするように編集されました – prasanth

3

あなたは、同じIDを持つことはできません。 idを別のものに変更する。

<button id="watchbutton">Watch Trailer &#9658</button> 
<br> 
<button id="watchbutton2">Watch Trailer &#9658</button> 
+0

IDはCSS + JSにリンクされているため、CSS + JSの各ボタンのIDを変更することはできません。私のページは非常に遅くなります。私のページには少なくとも20個のボタンがあります。 –

+0

あなたはidを使わずにクラスを使うべきです。 – tech2017

0

まず、同じIDを使用することをお勧めではありません。

<button id="watchbutton">Watch Trailer &#9658</button> 

そして、あなたはあなたが唯一のボタン(最初の)を取得

var btn = document.getElementById("watchbutton"); 

を使用する場合、あなたが好きなものを使用する必要があります

<button id="watchbutton" class="watchButton">Watch Trailer &#9658</button> 
<br> 
<button id="watchbutton2" class="watchButton" >Watch Trailer &#9658</button> 

をそしてそれらを取得する:

var buttons = document.getElementsByClassName("watchButton"); 
1

あなたはほぼそこにいます。

Frits、prasad &他の2 idsの問題を修正した後でも、プッシュするボタンに応じて別のビデオを再生するよう依頼する必要があります(playVideo)。

ここでは、望ましい結果を達成するようにコードを修正することができます方法は次のとおりです。私はこれが唯一の問題であることは言わないよ

// Get the modal 
 
var modal = document.getElementById('trailerdivbox'); 
 

 
// Get the button that opens the modal 
 
var btn1 = document.getElementById("TDwJDRbSYbw"); 
 
var btn2 = document.getElementById("6H_0aem12_I"); 
 

 
function playVideo(id) { 
 
    var video = document.getElementById('video'); 
 
    var src = video.dataset.src + id; 
 

 
    video.src = src + '?autoplay=1'; 
 
} 
 

 
function resetVideo() { 
 
    var video = document.getElementById('video'); 
 
    var src = video.src.replace('?autoplay=1', ''); 
 

 
    video.src = ''; 
 
    video.src = src; 
 
} 
 

 
// When the user clicks the button, open the modal 
 
btn1.onclick = function(e) { 
 
    modal.style.display = "block"; 
 
    playVideo(this.id); 
 
} 
 

 
btn2.onclick = btn1.onclick; 
 

 
var trailerbox = document.getElementById("close"); 
 

 
trailerbox.onclick = function() { 
 
    modal.style.display = "none"; 
 
    resetVideo(); 
 
} 
 

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target == modal) { 
 
    modal.style.display = "none"; 
 
    resetVideo(); 
 
    } 
 
}
/* Watch Trailer Button CSS */ 
 

 
.btn { 
 
    background-color: #f2f2f2; 
 
    color: red; 
 
    font-weight: 600; 
 
    border: none; 
 
    /* This one removes the border of button */ 
 
    padding: 10px 12px; 
 
} 
 

 
.btn:hover { 
 
    background-color: #e2e2e2; 
 
    cursor: pointer; 
 
} 
 

 
#trailerdivbox { 
 
    display: none; 
 
    width: 100%; 
 
    height: 100%; 
 
    position: fixed; 
 
    overflow: auto; 
 
    /* Enable Scrolling */ 
 
    background-color: rgb(0, 0, 0); 
 
    /* Fallback color */ 
 
    background-color: rgba(0, 0, 0, 0.4); 
 
    /* Black w/ opacity */ 
 
} 
 

 
.videoWrapper { 
 
    position: relative; 
 
    padding-bottom: 56.25%; 
 
    /* 16:9 */ 
 
    padding-top: 25px; 
 
    height: 0; 
 
} 
 

 
.videoWrapper iframe { 
 
    position: absolute; 
 
    max-width: 560px; 
 
    max-height: 315px; 
 
    width: 95%; 
 
    height: 95%; 
 
    left: 0; 
 
    right: 0; 
 
    margin: auto; 
 
}
<button class="btn" id="TDwJDRbSYbw">Watch Trailer &#9658</button> 
 
<br> 
 
<button class="btn" id="6H_0aem12_I">Watch Trailer &#9658</button> 
 

 
<div id="close"> 
 
    <div id="trailerdivbox"> 
 
<div class="videoWrapper"> 
 
    <iframe id="video" class="trailervideo" width="560" height="315" data-src="https://www.youtube.com/embed/" src="about:blank" frameborder="0" allowfullscreen></iframe> 
 
</div> 
 
    </div> 
 
</div> 
 
<br>


See also this JSFiddle demo

+0

JSで複数の変数を作成せずに彼らの任意のソリューションですか?私のページには20個のトレーラーボタンがあり、20個のボタンをPHPスクリプトで作成するので、20個のJavascriptを作成する必要があるので、ページが遅くなります –

+0

Prasadのソリューションのように –

+0

'

関連する問題