jquery
  • append
  • clone
  • addclass
  • 2011-07-21 90 views 0 likes 
    0

    在下面的示例jQuery中,在顯示克隆的HTML段之前,如何將文本「Remove Frog」更改爲「Remove Frog#99」並添加一個名爲「frog99」的ID 「,其中99是一個測序編號?克隆,附加和修改(青蛙)

    <div id='imageBox'> 
        </div> 
    
        <div class="hidden" id='template'> 
         <span class='alignRight'><a href='#' class='deleteFrog'>Remove Frog</a></span> 
         <span class='alignRight'><a href='#' class='addFrog'>Add Frog</a></span> 
         <img src="img/frog.jpg"> 
        </div> 
    

    腳本:

    var frogCount = 0; 
    
    $(document).ready(function() { 
        // listener: add "live" listener to link 
        $("#addFrog").live('click', function() { 
         // Clone hidden template 
         myClone = $("#template").clone(); 
         // Before showing cloned object, I would like to change text "Remove Frog" to "Remove Frog #99". 
         // I think it should look something like this, 
         myClone.("a").text("Remove Frog #" + ++frogCount); 
    
         // Also add an id called "frog99", where 99 is a sequential number. 
    
         myClone.removeClass("hidden"); 
         $('#imageBox').append(myClone); 
        }); 
    }); 
    
    +1

    ,你的問題是什麼? –

    回答

    0

    更改線路

    myClone.("a").text("Remove Frog #" + ++frogCount); 
    

    myClone.find("a.deleteFrog") 
        .text("Remove Frog #" + ++frogCount) 
        .attr('id','frog'+frogCount); 
    
    +0

    有點冗長,但那就是訣竅。謝謝! – bob

    相關問題