<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<script type="text/javascript">
$(function()
{
$("p").text(function(index, oldText) {
return "Existing text: " + oldText + ". New text: A dynamically set text (#" + index + ")";
});
});
</script>
结果:
Existing text: Paragraph 1. New text: A dynamically set text (#0)
Existing text: Paragraph 2. New text: A dynamically set text (#1)
Existing text: Paragraph 3. New text: A dynamically set text (#2)
问题:
如果我只想将其应用于第 1 段和第 2 段(以下是我想要的结果),我该怎么办?
Existing text: Paragraph 1. New text: A dynamically set text (#0)
Existing text: Paragraph 2. New text: A dynamically set text (#1)
Paragraph 3.