2017-08-17 19 views
-3

我無法找到如何當我點擊進入圓圈再次改變attr對於這兩個attr。我很明白新的代碼!謝謝大家的快速響應。在jQuery我無法找到如何改變attr一次又一次

<body> 

<iframe width="560" height="315" src="https://www.youtube.com/embed/5SgyFaBS7WI" frameborder="0" allowfullscreen></iframe> 

    <div id="circle"></div> 


    <script type="text/javascript"> 

     $("#circle").click(function() { 

      $("iframe").attr("src", <iframe width="560" height="315" src="https://www.youtube.com/embed/0WazFtJSIHk" frameborder="0" allowfullscreen></iframe>, function("src","https://www.youtube.com/embed/5SgyFaBS7WI"); 

     }); 



    </script> 

</body> 

+2

您對'attr'的值應該用單引號引起來。 –

+2

你現在甚至不會使用最基本的JavaScript語法,否則你將不得不知道,這段代碼完全是無稽之談。 – CBroe

+0

你在javascript調試控制檯中是否有任何錯誤? – Andy

回答

0
<body> 

<iframe width="560" height="315" src="https://www.youtube.com/embed/5SgyFaBS7WI" frameborder="0" allowfullscreen></iframe> 

<div id="circle">Click me</div> 

<script 
    src="https://code.jquery.com/jquery-2.2.4.min.js" 
    integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" 
    crossorigin="anonymous"></script> 
<script type="text/javascript"> 

    $(document).ready(function() { 
     var links = [ 
      "https://www.youtube.com/embed/0WazFtJSIHk", 
      "https://www.youtube.com/embed/5SgyFaBS7WI" 
     ]; 

     var counter = 0; 

     $("#circle").click(function() { 
      $("iframe").attr("src", links[counter % links.length]); 
      counter++; 
     }); 
     }); 

</script> 

</body> 
+0

非常感謝你! – Leandros

+0

您可以爲我的答案投票嗎? –

+0

@AlexanderBolzhatov精確地說,'links [counter%2]'應該是'links [counter%links.length]'。 – tilz0R

0

你做錯了。

jQuery('iframe').attr('src', 'http://www.youtube.com/...'); 

僅添加youtube URL,而不是整個HTML代碼。

+0

謝謝!我的錯誤:)!新的代碼! – Leandros

+0

如果有效,請標記爲正確答案。 @Leandros – tilz0R