开发者

Help in jquery for dynamic div and p

I am having a problem which i am not able to understand at the moment. I have a script in jsp which creates dynamic <p> and <div> tags. Each <p> tag has class 'parent' and each div class has class 'child'. Now what I want to achieve is, when a user clicks on a particular <p> tag then the <div> which is just under that <p> should appear. But when 开发者_如何学JAVAI click on <p> tag now..its opening all the divs..of other <p> tags too. Kindly help me with this. thanks

Thank you guys...I made few changes in the code..and its working fine now.

$(document).ready(function(){
$(".child").hide();
$(".parent").bind('click',function(){



$(this).next('.child').toggle("slow");


    });

});


In jQuery

$('p.parent').click(function(){ $(this).next('.child').show(); });

The .next() method tells jQuery to select the "immediately following sibling", which in your case would be the div.


You have to use children selectors in jquery: http://api.jquery.com/category/traversing/tree-traversal/

I can't answer without the code but here's my try:

$('p').click(function(){
    $(this).find('div').show();
});

this works if the code, wrongly, is something like this: <p><div></div></p>.

If you have the code <p></p><div></div>, use this:

$('p').click(function(){
    $(this).next().show();
});


when you use a jquery selector to get a p like this:

$('p.parent')

it will select ALL p tags with class parent. You will have to be more specific on selecting $('p.parent, .child:first') for example will select the first one, check out http://api.jquery.com/category/selectors/ for all selectors in jquery.

When you want to select the p that is clicked on, you simply use $(this).

$('p.parent').bind('click', function() {
$(this).children('div').show(); // look at how you can use $(this) which will link to the p clicked on
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜