【问题标题】:Cloning, appending and modifying (frogs)克隆、追加和修改(青蛙)
【发布时间】:2011-10-10 03:55:22
【问题描述】:

在下面的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);
    });
});

【问题讨论】:

    标签: jquery append clone addclass


    【解决方案1】:

    换行

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

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

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 1970-01-01
      • 2016-07-01
      • 2018-02-11
      • 2011-10-22
      • 2014-11-16
      • 2013-03-27
      • 2021-08-30
      • 1970-01-01
      相关资源
      最近更新 更多