简体   繁体   中英

JavaScript/jQuery How do you select all Audio Elements created dynamically?

I am created elements via javascript in the document.ready.

How do I select all the elements that I created? I can't find a simple/working answer to this anywhere!

Here is what I tried, but it is NOT working!

$(document).ready(function () {

$.audioElement1 = document.createElement('audio');
$.audioElement1.setAttribute('src', '/Content/mfile1.mp3');

$.audioElement2 = document.createElement('audio');
$.audioElement2.setAttribute('src', '/Content/mfile2.mp3');

});



function stopAll()
        {
            var cnt = 0;
            $("audio").each(function () {
                cnt += 1;
                this.pause(); // Stop playing
                this.currentTime = 0; // Reset time
            });

            alert("total: " + cnt);
        }

My "alert" returns 0 every time/doesn't do anything!

You can use document.getElementsByTagName('audio') instead of $("audio") .

It returns an array of all audio elements.

HTML DOM getElementsbyTagName() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM