例1:2番目のpだけを選択したい場合。
<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タグにも適用可能。
例2:動的に生み出した3番目のpを選択
<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>
0 件のコメント:
コメントを投稿