$("body").on("click", "button", audioelement.play());
まず最初は期待どおりにボタンのハンドラが実行されません - それはしようとします直接呼び出すのですぐにplay
メソッドを起動してください。次に、play
メソッドをaudio要素で呼び出す必要があり、JQuery要素では呼び出されないため、...audioelement.play is not a function
というエラーが発生します。
このお試しください:
$("body").on("click", "button", function() { audioelement[0].play(); });
編集:オリジナルのポスターの意図は、スクリプトタグがWYSIWG設定のいくつかの並べ替えではスニペットとして再利用できることです、フィドル+を念頭に置いてこれを更新スニペット。
Click to see fiddle
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="http://www.freesfx.co.uk/rx2/mp3s/5/16797_1460740933.mp3">SOUND-1</a>
<script>
// Add buttons and otherwise manipulate the dom
function LinkToAudio(p_element, p_callback) {
var audioURL = $(p_element).attr('href');
var audioName = $(p_element).text();
// Dom for the audio tag
var audioTag = '<audio src="' + audioURL + '" type="audio/mpeg" preload="none"></audio>';
// Dom for the play button
var audioPlayButton = '<button id="audio">Play Sound above</button>';
// Add a audio tag and play button after the element
$(p_element).after('<br/>' + audioTag + '<br/>' + audioPlayButton + '<br/>');
\t // Add the callback to the button
$(p_element).nextAll('button:first').on('click', p_callback);
}
// Define our button click handler
function AudioButtonHandler(evt) {
\t // Once clicked, find the first audio tag located before the button
\t var relativeAudioTag = $(evt.target).prevAll('audio:first')[0];
\t console.log(evt.target, relativeAudioTag);
// Play the sound!
relativeAudioTag.play();
}
var thisAudio = $('a[href$="mp3"]:last');
LinkToAudio(thisAudio, AudioButtonHandler);
</script>
<a href="http://www.freesfx.co.uk/rx2/mp3s/5/16983_1461335348.mp3">SOUND-2</a>
<a href="http://www.freesfx.co.uk/rx2/mp3s/4/16539_1460655601.mp3">SOUND-3</a>
<script>
// Add buttons and otherwise manipulate the dom
function LinkToAudio(p_element, p_callback) {
var audioURL = $(p_element).attr('href');
var audioName = $(p_element).text();
// Dom for the audio tag
var audioTag = '<audio src="' + audioURL + '" type="audio/mpeg" preload="none"></audio>';
// Dom for the play button
var audioPlayButton = '<button id="audio">Play Sound above</button>';
// Add a audio tag and play button after the element
$(p_element).after('<br/>' + audioTag + '<br/>' + audioPlayButton + '<br/>');
\t // Add the callback to the button
$(p_element).nextAll('button:first').on('click', p_callback);
}
// Define our button click handler
function AudioButtonHandler(evt) {
\t // Once clicked, find the first audio tag located before the button
\t var relativeAudioTag = $(evt.target).prevAll('audio:first')[0];
\t console.log(evt.target, relativeAudioTag);
// Play the sound!
relativeAudioTag.play();
}
var thisAudio = $('a[href$="mp3"]:last');
LinkToAudio(thisAudio, AudioButtonHandler);
</script>
ありがとうございますが、このフィドルhttps://jsfiddle.net/jfyeqy7d/2/に私は第三のサウンドファイル以下のスクリプトのクローンを挿入します。間違って、生成されたボタンは、それらのサウンドファイルではなく、同じサウンドファイルを再生しています。 audioelement [0]を考えてください。プレイ()は犯罪者です... – Madamadam
あなたが提供したフィドルで、私はまだインライン関数の呼び出しを参照してください。上記で提供した無名関数を使ってみましたか?あなたのフィドルをフォークし、16行目のボタンハンドラを置き換えました。 https://jsfiddle.net/wymy1abv/1/ また、スクリプトは、タグを使ってその位置に関連する最後のオーディオタグを見つけ出し、最後のタグが見つかるようにするには、スクリプトが動いた後にスクリプトを動かす必要があります。 https://jsfiddle.net/drscodelab/27u6eod8/ – DabaloSe7eN
domの相対位置に関係なく、最後のa/audioタグをスクリプトで検索する場合は、ドキュメントが準備されるまで待つことができます最後のインスタンスを見つける。これが現在の設定とぶつからない場合は、この方法をお勧めします。このフィドルを見てください - https://jsfiddle.net/drscodelab/0s0h9bga/ – DabaloSe7eN