JQuery为每个跨度追加子项

JQuery为每个跨度追加子项,jquery,Jquery,我有以下HTML代码: <div class="truc"> <span class="test" date-url="12.mp3"></span> </div> <div class="truc"> <span class="test" date-url="64.mp3"></span> <div> 我想在span中添加一个audio标记,src属性是span标记的datau

我有以下HTML代码:

<div class="truc">
    <span class="test" date-url="12.mp3"></span>
</div>
<div class="truc">
    <span class="test" date-url="64.mp3"></span>
<div>

我想在span中添加一个audio标记,src属性是span标记的dataurl属性。 我编写了以下代码:

$('span.test').each(function(){
    var ajout = '<audio><source src="'+$(this).attr("data-url")+'"></audio>';
    $(this).append(ajout);
});
$('span.test')。每个(函数(){
var ajout='';
$(this.append(ajout);
});

它可以工作,但没有优化,因为append位于“each”循环中。我正在努力寻找更好的写作方法。也许你能帮我?非常感谢。

您可以使用的一个调整是将append与回调一起使用,类似于,没有太多优化,但不太详细

$('span.test').append(函数(){
返回“”;
});

您可以使用的一个调整是将append与回调一起使用,类似于,没有太多优化,但不太详细

$('span.test').append(函数(){
返回“”;
});

您可以使用的一个调整是将append与回调一起使用,类似于,没有太多优化,但不太详细

$('span.test').append(函数(){
返回“”;
});

您可以使用的一个调整是将append与回调一起使用,类似于,没有太多优化,但不太详细

$('span.test').append(函数(){
返回“”;
});

我建议如下:

// create a single instance of the element(s) you
// wish to append:
var newHTML = $('<audio />', {
    'html' : '<source></source'
}),
// create an unitialised variable to use within 
// within the (inevitable) loop:
    clone;

// select the element(s) to which you wish
// to append new content:
$('span.test').append(function() {

  // create a reference to the cloned element
  // (to avoid recreating the same element(s)
  // every iteration):
  clone = newHTML.clone(true);

  // find the <audio> element within the
  // cloned element, and update its 'src'
  // property; within this loop 'this'
  // refers to the current span.test element
  // over which we're iterating:
  clone.find('audio').prop('src', this.dataset.url);

  // return the cloned, and updated, elements to
  // the append method:
  return clone;
});
//创建所需元素的单个实例
//谨附上:
var newHTML=$(')

  • jQuery:

    • 我建议如下:

      // create a single instance of the element(s) you
      // wish to append:
      var newHTML = $('<audio />', {
          'html' : '<source></source'
      }),
      // create an unitialised variable to use within 
      // within the (inevitable) loop:
          clone;
      
      // select the element(s) to which you wish
      // to append new content:
      $('span.test').append(function() {
      
        // create a reference to the cloned element
        // (to avoid recreating the same element(s)
        // every iteration):
        clone = newHTML.clone(true);
      
        // find the <audio> element within the
        // cloned element, and update its 'src'
        // property; within this loop 'this'
        // refers to the current span.test element
        // over which we're iterating:
        clone.find('audio').prop('src', this.dataset.url);
      
        // return the cloned, and updated, elements to
        // the append method:
        return clone;
      });
      
      //创建所需元素的单个实例
      //谨附上:
      var newHTML=$(')
      
  • jQuery:

    • 我建议如下:

      // create a single instance of the element(s) you
      // wish to append:
      var newHTML = $('<audio />', {
          'html' : '<source></source'
      }),
      // create an unitialised variable to use within 
      // within the (inevitable) loop:
          clone;
      
      // select the element(s) to which you wish
      // to append new content:
      $('span.test').append(function() {
      
        // create a reference to the cloned element
        // (to avoid recreating the same element(s)
        // every iteration):
        clone = newHTML.clone(true);
      
        // find the <audio> element within the
        // cloned element, and update its 'src'
        // property; within this loop 'this'
        // refers to the current span.test element
        // over which we're iterating:
        clone.find('audio').prop('src', this.dataset.url);
      
        // return the cloned, and updated, elements to
        // the append method:
        return clone;
      });
      
      //创建所需元素的单个实例
      //谨附上:
      var newHTML=$(')
      
  • jQuery:

    • 我建议如下:

      // create a single instance of the element(s) you
      // wish to append:
      var newHTML = $('<audio />', {
          'html' : '<source></source'
      }),
      // create an unitialised variable to use within 
      // within the (inevitable) loop:
          clone;
      
      // select the element(s) to which you wish
      // to append new content:
      $('span.test').append(function() {
      
        // create a reference to the cloned element
        // (to avoid recreating the same element(s)
        // every iteration):
        clone = newHTML.clone(true);
      
        // find the <audio> element within the
        // cloned element, and update its 'src'
        // property; within this loop 'this'
        // refers to the current span.test element
        // over which we're iterating:
        clone.find('audio').prop('src', this.dataset.url);
      
        // return the cloned, and updated, elements to
        // the append method:
        return clone;
      });
      
      //创建所需元素的单个实例
      //谨附上:
      var newHTML=$(')
      
  • jQuery:

  • 属性中应该是
    数据url
    而不是
    日期url
    。。除此之外,没有什么可以做的。当您必须
    将它附加到多个元素上时,您应该这样做。没有更好的方法了!属性中应该是
    数据url
    而不是
    日期url
    。。其他当你必须
    将它附加到多个元素上时,这就是你应该做的事情。没有更好的方法了!属性中应该是
    数据url
    而不是
    日期url
    。。除此之外,没有什么可以做的事情,这就是你必须
    应用程序时应该做的事情在多个元素上结束它。
    没有更好的方法了!属性中应该是
    数据url
    而不是
    日期url
    。。除此之外,没有什么可以做的。当你必须
    在多个元素上追加它时,这就是你应该做的。没有更好的方法了!