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