2017-10-18 71 views
0

我希望使用字符串'activeVideo'作爲變量,但我無法讓它工作。我覺得我很接近做正確的事情,但我現在一直堅持幾個小時......我做錯了什麼?我如何正確創建一個字符串在JavaScript/jQuery中的變量?

firstVideo = 'https://www.youtube.com/embed/kxopViU98Xo?rel=0&showinfo=0?ecver=2;autoplay=1'; 
 
secondVideo = 'https://www.youtube.com/embed/FWO5Ai_a80M?ecver=2;autoplay=1'; 
 
jQuery('.play-circle').click(function() { 
 
    console.log(this.id + 'Video'); 
 
    activeVideo = this.id + 'Video'; 
 
    jQuery('.play-circle').fadeOut(0); 
 
    jQuery('.video').fadeIn(0); 
 
    jQuery('.video-close').fadeIn(0); 
 
    jQuery('#' + this.id + 'Video').attr('src', activeVideo); 
 
}); 
 
jQuery('.video-close').click(function() { 
 
    console.log('closing video'); 
 
    jQuery(activeVideo).attr('src', ''); 
 
    jQuery('.video').fadeOut(0); 
 
    jQuery('.video-close').fadeOut(0); 
 
    jQuery('.play-circle').fadeIn(0); 
 
});
body { 
 
    background-color: #000000; 
 
} 
 

 
.banner-wide { 
 
    position: relative; 
 
    height: 720px; 
 
    background: url('https://picsum.photos/1000/500') center/cover; 
 
} 
 

 
.banner-wide .text { 
 
    width: 30%; 
 
    position: relative; 
 
    top: 50%; 
 
    transform: translateY(-50%); 
 
    margin-left: 7%; 
 
} 
 

 
.banner-wide .text h3, 
 
.banner-wide .text p { 
 
    text-align: left; 
 
    color: #ffffff; 
 
} 
 

 
.video-thumbnail { 
 
    z-index: 11; 
 
    position: relative; 
 
    right: 0; 
 
    top: 0; 
 
    width: 50%; 
 
    height: 360px; 
 
    cursor: pointer; 
 
    float: right; 
 
    padding: 5px; 
 
    box-sizing: border-box; 
 
} 
 

 
.play-circle { 
 
    width: 100%; 
 
    height: 100%; 
 
    background: url('http://addplaybuttontoimage.way4info.net/Images/Icons/13.png') no-repeat center/150px rgba(0, 0, 0, 0.7); 
 
} 
 

 
.video { 
 
    display: none; 
 
    width: 100%; 
 
    height: 360px; 
 
    position: relative; 
 
    right: 0; 
 
    top: 0; 
 
} 
 

 
.video-close { 
 
    display: none; 
 
    position: absolute; 
 
    width: 20px; 
 
    height: 20px; 
 
    top: 14px; 
 
    right: 20px; 
 
    cursor: pointer; 
 
    background: url('http://freevector.co/wp-content/uploads/2013/03/8934-close-button1.png') no-repeat center center/20px rgba(255, 255, 255, 1); 
 
    border-radius: 100px; 
 
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="banner-wide owc"> 
 
    <div class="video-thumbnail"> 
 
    <div id="first" class="play-circle"></div> 
 
    <div class="video-close"></div> 
 
    <iframe class="video" id="firstVideo" width="375" height="225" frameborder="0" allowfullscreen></iframe> 
 
    </div> 
 
    <div class="video-thumbnail"> 
 
    <div id="second" class="play-circle"></div> 
 
    <div class="video-close"></div> 
 
    <iframe class="video" id="secondVideo" width="375" height="225" frameborder="0" allowfullscreen></iframe> 
 
    </div> 
 
</div>

+1

你只是不要。至少不*正確* –

+0

請不要鏈接到第三方網站,因爲這些鏈接可能會隨着時間的推移而死亡。只需在代碼片段中發佈您的代碼就可以了。 –

+1

只需將'activeVideo = this.id +'Video';'放入關閉函數中,它就可以工作。並且請注意,您需要聲明變量!! **,爲此使用'const','let','var'。然後你的代碼會在這種情況下崩潰,你可以回到正確的軌道 –

回答

0

字符串不可執行,不能用於代替評估代碼。但是,對象結構只不過是鍵(即字符串)和值,所以您可以將字符串(通過數組索引符號)傳遞給對象以提取該鍵的值。

既然你正在構建對應元素的id的字符串,可以傳遞字符串作爲「鑰匙」 window對象,它將解析正確的對象,因爲與id小號元素成爲全局屬性。

然後,如果將URL存儲到另一個對象中的視頻中,則可以將該字符串傳遞給該對象以提取所需的URL。

你還需要聲明你的變量並給它們適當的範圍。

$(function(){ 
 

 
    // By storing the data in an object with key/value pairs, the key names can be 
 
    // accessed via the scope of the object later 
 
    var obj = { 
 
    firstVideo : 'https://www.youtube.com/embed/kxopViU98Xo?rel=0&amp;showinfo=0?ecver=2;autoplay=1', 
 
    secondVideo : 'https://www.youtube.com/embed/FWO5Ai_a80M?ecver=2;autoplay=1' 
 
    } 
 

 
    var activeVideo = ""; // Need to declare this in a scope that can be accessed from both functions 
 

 
\t \t jQuery('.play-circle').click(function(){ 
 

 
     activeVideo = this.id + 'Video';  
 
     jQuery('.play-circle').fadeOut(0); 
 
\t \t \t jQuery('.video').fadeIn(0); 
 
\t \t \t jQuery('.video-close').fadeIn(0); 
 
     
 
     // Get the element stored in the global property and pass to jQuery, then 
 
     // get the key out of the storage object and use as the source: 
 
\t \t \t jQuery(window[activeVideo]).attr('src', obj[activeVideo]); 
 
     // Test: 
 
\t \t \t console.log(jQuery(window[activeVideo]).attr('src')); 
 
\t \t }); 
 

 
\t \t jQuery('.video-close').click(function(){ 
 
\t \t \t console.log('closing video'); 
 
     // Elements with id's become global properties, so the string 
 
     // of their id can be passed as a key to the window object: 
 
\t \t \t $(window[activeVideo]).attr('src',''); 
 
\t \t \t jQuery('.video').fadeOut(0); 
 
\t \t \t jQuery('.video-close').fadeOut(0); 
 
\t \t \t jQuery('.play-circle').fadeIn(0); 
 
\t \t }); 
 
});
body { 
 
    background-color: #000000; 
 
} 
 
.banner-wide { 
 
\t position: relative; 
 
\t height: 720px; 
 
\t background: url('https://picsum.photos/1000/500') center/cover; 
 
} 
 
.banner-wide .text { 
 
\t width: 30%; 
 
\t position: relative; 
 
\t top: 50%; 
 
\t transform: translateY(-50%); 
 
\t margin-left: 7%; 
 
} 
 
.banner-wide .text h3, 
 
.banner-wide .text p { 
 
\t text-align: left; 
 
\t color: #ffffff; 
 
} 
 
.video-thumbnail { 
 
\t z-index: 11; 
 
\t position: relative; 
 
\t right: 0; 
 
\t top: 0; 
 
\t width: 50%; 
 
\t height: 360px; 
 
\t cursor: pointer; 
 
    float: right; 
 
    padding: 5px; 
 
    box-sizing: border-box; 
 
} 
 
.play-circle { 
 
\t width: 100%; 
 
\t height: 100%; 
 
    background: url('http://addplaybuttontoimage.way4info.net/Images/Icons/13.png') no-repeat center/150px rgba(0,0,0,0.7); 
 
} 
 
.video { 
 
\t display: none; 
 
\t width: 100%; 
 
\t height: 360px; 
 
\t position: relative; 
 
\t right: 0; 
 
\t top: 0; 
 
} 
 
.video-close { 
 
\t display: none; 
 
\t position: absolute; 
 
\t width: 20px; 
 
\t height: 20px; 
 
\t top: 14px; 
 
\t right: 20px; 
 
\t cursor: pointer; 
 
\t background: url('http://freevector.co/wp-content/uploads/2013/03/8934-close-button1.png') no-repeat center center/20px rgba(255,255,255,1); 
 
\t border-radius: 100px; 
 
\t box-shadow: 0 0 20px rgba(0,0,0,0.1); 
 
\t z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="banner-wide owc"> 
 
     <div class="video-thumbnail"> 
 
      <div id="first" class="play-circle"></div> 
 
      <div class="video-close"></div> 
 
      <iframe class="video" id="firstVideo" width="375" height="225" frameborder="0" allowfullscreen></iframe> 
 
     </div> 
 
    <div class="video-thumbnail"> 
 
      <div id="second" class="play-circle"></div> 
 
      <div class="video-close"></div> 
 
      <iframe class="video" id="secondVideo" width="375" height="225" frameborder="0" allowfullscreen></iframe> 
 
     </div> 
 
    </div>

+0

感謝您的所有建議。這是我需要的東西。再次,非常感謝! :) – alc

+0

嗯好的。我剛發現這個解決方案在iOS上不起作用。你知道這可能是爲什麼嗎? – alc

+0

@alc您在開發者控制檯中看到什麼消息。在這段代碼中,我想不出任何非標準的東西。 –

-2

這將修復它...

jQuery('#' + this.id + 'Video').attr('src',eval(activeVideo)); 

免責聲明:正如你從下面的評論中看到,它不是流行。我對此很清楚。你可以停止投票。如果你是那些相信eval()是邪惡的人,請隨時不要使用它。但這是一個可行的解決方案,可以快速輕鬆地解決問題,而不使用它(恕我直言)將是一個過早的優化。當然可以自由地做自己的研究,以決定是否走這條路。

+2

'eval()'從來不是解決任何問題的好辦法。 –

+0

這將回答這個問題,但不是。 –

+0

@ScottMarcus我不同意這個答案,但這是不正確的。如果使用得當,這很好。我能想到的一個例子就是構建一個模板引擎。 – zfrisch

1

而不是兩個單獨的參數,你可以這樣定義一個對象:

var vids = {first: 'https://www.youtube.com/embed/kxopViU98Xo?rel=0&amp;showinfo=0?ecver=2;autoplay=1', 
      second: 'https://www.youtube.com/embed/FWO5Ai_a80M?ecver=2;autoplay=1'}; 

然後使用該對象與ID爲關鍵索引到它:

jQuery('.play-circle').click(function(){   
    jQuery('.play-circle').fadeOut(0); 
    jQuery('.video').fadeIn(0); 
    jQuery('.video-close').fadeIn(0); 
    jQuery('#' + this.id + 'Video').attr('src',vids[this.id]); 
}); 
相關問題