2014-02-13 32 views
1

我是JQuery的新手,這是一個基本問題,我不明白爲什麼下面的代碼不起作用。請幫我弄明白。在HTML5中使用「音頻」元素的JQuery

<!DOCTYPE html> 
    <html> 
    <head> 
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"> 
    </script> 
    <script> 

     function test() 
     { 
      var audios=$("#audio1"); 
      alert(audios.volume); //This line does not work, it returns "undefined" 
      audios.volume=0.5; //This line does not work 
      // audios.hide(); - This works fine 
     } 
    </script> 
    <title>This is a test</title> 
    </head> 

    <body> 
       <input type="button" value="Test" onclick="test()"> 
       <audio id="audio1" controls autoplay> 
        <source src="http://zz.qz828.net/06/three.mp3" type="audio/mpeg"> 
        Your browser does not support the audio element. 
       </audio> 
    </body> 
    </html> 

不知道爲什麼這兩行不行:

alert(audios.volume); //This line does not work 
audios.volume=0.5; //This line does not work 

謝謝!

回答

2

您需要將volume屬性設置爲dom元素,$("#audio1")返回沒有volume屬性的jQuery對象。

所以,你需要從jQuery對象訪問DOM元素,然後設置屬性或使用.prop()設置屬性值

audios[0].volume=0.5; 
//or audios.prop('volume', 0.5); 
+0

好吧,我試過了,它的工作原理。謝謝。 –

+0

看起來像jquery對象與DOM對象有區別,對吧? –

+0

@JackZhang它返回一個[jQuery對象](http://learn.jquery.com/using-jquery-core/jquery-object/)不是一個DOM元素 –