在下面的jQuery示例中,在显示克隆的HTML片段之前,如何将“删除青蛙”文本更改为“删除青蛙#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);
});
});
答案 0 :(得分:0)
更改行
myClone.("a").text("Remove Frog #" + ++frogCount);
到
myClone.find("a.deleteFrog")
.text("Remove Frog #" + ++frogCount)
.attr('id','frog'+frogCount);