2013-03-16 31 views
5

這是我第一篇帖子到論壇。 我正在建立一個英文/泰文課程網站,其中將包含許多英文和泰文演講的音頻文件。我使用鼠標懸停/按鈕做了一個網頁的概念驗證。javascript函數 http://www.javascriptkit.com/script/script2/soundlink.shtml 對於一些mod和一些內聯js,這個工作正常。將javascript音頻移動到jquery中

$(document).ready(function() { 
    var html5_audiotypes = { 
    "mp3": "audio/mpeg", 
    "ogg": "audio/ogg", 
    };  
function createsoundbite(sound) { 
    "use strict"; 
    var html5audio = document.createElement('audio'); 
    if (html5audio.canPlayType){ 
    for(var i=0; i<arguments.length; i++) { 
    var sourceEl = document.createElement('source'); 
    sourceEl.setAttribute('src', arguments[i]); 
    if (arguments[i].match(/\.(\w+)$/i)) 
    sourceEl.setAttribute('type', html5_audiotypes[RegExp.$1]); 
    html5audio.appendChild(sourceEl); 
    } 
    html5audio.load(); 
    html5audio.playclip = function() { 
    html5audio.pause(); 
    html5audio.currentTime=0; 
    html5audio.play(); 
    } 
    return html5audio; 
    } 
    else{ 
    return {playclip:function(){throw new Error("Your browser doesn't support  HTML5 audio unfortunately")}}; 
    } 
    } 
    var cn402=createsoundbite("audio/cn402.ogg", "audio/cn402.mp3"); 
    var ry402=createsoundbite("audio/ry402.ogg", "audio/ry402.mp3");  
    var cn403=createsoundbite("audio/cn403.ogg", "audio/cn403.mp3");  
    var ry403=createsoundbite("audio/ry403.ogg", "audio/ry403.mp3");  
    var cn404=createsoundbite("audio/cn404.ogg", "audio/cn404.mp3");  
    var ry404=createsoundbite("audio/ry404.ogg", "audio/ry404.mp3");  
}); 

的JavaScript代碼是通過這樣的HTML代碼行稱爲:

<a href="#current" class= "rollover" onclick="ry402.playclip()"></a>Please don't swim by yourself</span> 

我想將這一功能及其相關變量到jQuery的。這樣所有的js代碼都會從html中刪除。第一步,讓jquery代碼運行起來對我來說已經證明有點問題。我已經嘗試在文檔準備功能中包含js音頻代碼。如上所示,但這不起作用。任何幫助將非常感激。

+0

看過來HTTP ://stackoverflow.com/questions/6830445/extending-jquery – Bart 2013-03-16 14:19:59

回答

3

我不知道如果我得到你的權利,但你可以很輕鬆地刪除從給定鏈路的JavaScript代碼:

$('.rollover').click(function(){ 

ry402.playclip(); 

}); 

讓我們一起來看看:http://api.jquery.com/click/

+0

MeierI將這段代碼放在jq_click.js中,並將其定位爲: RoyS 2013-03-16 18:07:04

+0

這不起作用,我把代碼放在$(document).ready(function(){}中,並將其放在加載jquery的腳本的head部分中。對於這篇文章,我不熟悉Stack Overflow論壇 – RoyS 2013-03-16 18:23:43