Как выбрать и получить атрибуты видео и аудио элемента при переборе DOM в jQuery - PullRequest
2 голосов
/ 06 апреля 2020

Я пробовал это для данного html элемента.

    $('#dynamicformdata').each(function() {

          console.log(this.type);
     })

  <div id="dynamicformdata">
    <video width="320" height="240" >
        <source src="movie.mp4" type="video/mp4">
        <source src="movie.ogg" type="video/ogg">

   </video>
 </div>

Как получить все атрибуты этого тега видео внутри div.

1 Ответ

2 голосов
/ 06 апреля 2020
    $('#dynamicformdata').each(function() {
      // "this" in the first function refers to the div
      console.log(this);

      // call a second function for every source tag in every video in the dynamicformdata tag
      $('video>source', this).each(function() {
        // "this" referes to a source tag
        console.log(this.type);
      });
    });

Если вы хотите получить все типы в виде массива:

    $('#dynamicformdata').each(function() {
      var types = [];
      $('video>source', this).each(function() {
        types.push(this.type);
      });
      // logs the div element and all found types (duplicates not filtered out)
      console.log(this, types);
    });
...