jquery
  • jquery-selectors
  • 2012-05-12 49 views 5 likes 
    5

    我有以下設置克隆 - >改變ID - >添加內容 - >顯示 - >附加到父

    <div class="article-content"> 
        <div class='editors' id="mine"> 
         <h2>My Text</h2> 
         <textarea name="my_text" id="my_text" class="my_test"></textarea> 
        </div> 
        <div class='editors' id="termsy_master" style="display:none;"> 
         <h2>title</h2> 
         <textarea name="" id="" class="sm">text</textarea> 
         <textarea name="" id="" class="di">text</textarea> 
        </div> 
    </div> 
    

    我嘗試

    • 克隆 「termsy_master」
    • 變化id到「his」
    • 將h2標題更改爲「新標題」
    • textarea sm id to「sm_new_id」
    • textarea的迪ID爲「di_new_id」
    • 刪除顯示沒有因此這將是可見的
    • 並將它添加到文章內容

    我有以下的已經做了,但是我不知道如何繼續

    $('.article-content').append($("#termsy_master").clone()); 
    

    回答

    16
    $tmc = $("#termsy_master").clone().attr('id', 'his').show(); 
    $("h2", $tmc).text('new title'); 
    $(".sm", $tmc).attr('id', 'sm_new_id'); 
    $(".di", $tmc).attr('id', 'di_new_id'); 
    $tmc.appendTo(".article-content"); 
    

    有很多,你可以做到這一點,當然方式,但是這似乎或多或少最簡單的給我。不要過分!

    +0

    完美啊!一切都有道理,謝謝! – seesoe

    3

    這裏是一個可能的解決方案:

    var block = $("#termsy_master"​).clone(); 
    block.children("h2").text("new title"); 
    block.children(".sm").attr("id", "sm_new_id"); 
    block.children(".di").attr("id", "di_new_id"); 
    block.attr("id", "his").show().appendTo(".article-content");​ 
    

    DEMO:http://jsfiddle.net/8pxPC/


    或者,如果你喜歡它在一個行:)

    $("#termsy_master").clone().attr("id", "his").show().appendTo(".article-content").children("h2").text("new title").siblings(".sm").attr("id", "sm_new_id").siblings(".di").attr("id", "di_new_id");​ 
    
    相關問題