1

我想在循环遍历 div 元素时检测 span 元素,即

<div class="example">
    These are some <span id ="special" class="anime">special</span> words 
   that should be faded in one after the other.
</div>

使用javascript我拆分单词并将它们一一淡入即

JS:

 function doSomething(spanID) {
     alert(spanID);
     }

 var $el = $(".example:first"), text = $.trim($el.text()),
        words = text.split(" "), html = "";

    for (var i = 0; i < words.length; i++) {
        html += "<span>" + words[i] + ((i+1) === words.length ? "" : " ") + "</span>";
    };
    $el.html(html).children().hide().each(function(i){
      // if special span is detected
      // get the span id pass it to doSomething function 
      $(this).delay(i*200).fadeIn(700);
    });
    $el.find("span").promise().done(function(){
        $el.text(function(i, text){
           return $.trim(text);
        });            
    });

工作示例在这里:http: //jsfiddle.net/6czap/5/

一切正常,只是我需要检测任何特殊的跨度,以便我可以对它们做一些事情,然后 for 循环应该继续做它 deos 的事情。谢谢

4

1 回答 1

1

尝试以下操作:

var orig = $(".example:first").html()
$el.html(html).children().hide().each(function(i){
  $(this).delay(i*200).fadeIn(700);
}).promise().done(function(){
   $('.example:first').html(orig);
});

演示!

于 2012-07-24T21:37:54.367 回答