使用jquery / js在循环时检测跨度?

时间:2012-07-24 21:04:26

标签: javascript jquery

我想在循环通过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循环应继续做它所做的事情。感谢

1 个答案:

答案 0 :(得分: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);
});

demo!