克隆、追加和修改(青蛙)

发布于 2024-11-25 10:33:11 字数 1210 浏览 0 评论 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);
    });
});

In the jQuery example below, before showing a cloned segment of HTML, how do I change the text "Remove Frog" to "Remove Frog #99" and also add an id called "frog99", where 99 is a sequencing number?

    <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>

Script:

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);
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-12-02 10:33:11

将行更改

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

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

Change the line

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

to

myClone.find("a.deleteFrog")
    .text("Remove Frog #" + ++frogCount)
    .attr('id','frog'+frogCount);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文