
それぞれに一個ずつ置きたいのに・・・。
こんなときは、map変数が二つのマップで共有されていないかチェック。
自分の場合はmapがグローバル変数になっていたので、これをローカル変数に変更したら、
下図のようにうまくいきました。
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button1").click(function(){
$("div.contents").prepend("<p>This is test.</p>");
$("p").attr("id",function(i){
return "num"+i;
});
});
$("#button2").click(function(){
$("p#num4").css("background-color","red");
});
});
</script>
</head>
<body>
<button id="button1">Click me</button>
<button id="button2">TEST</button>
<div class='contents'></div>
</body>
</html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('p:eq(1)').css("background-color","red");
});
</script>
</head>
<body>
<p>This is 1</p>
<p>This is 2</p>
<p>This is 3</p>
</body>
</html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#button1').click(function(){
$('.content').append('<p class="hello">Hello</p>');
});
$('#button2').click(function(){
$('p.hello:eq(2)').css("background-color","red");
});
});
</script>
</head>
<body>
<button id='button1'>add</button>
<button id='button2'>test</button>
<div class='content'></div>
</body>
</html>