PDA

View Full Version : مخفی کردن چندین DIV با یک کد



mohitlog
چهارشنبه 13 آبان 1394, 15:12 عصر
سلام

من تو صفحم چند تا زوج DIV با ID های مختلف دارم که میخام وقتی رو DIV اولی کلیک کرد DIV دومی باز یا بسته بشه.

من این کد رو نوشتم.
ولی تعداد توابعم خیلی زیاد شده.

ایا راهی هست که بشه با یک کد تمام کارم رو انچام بدم. مثلا بوسیله پارامتر ایدی Div رو به تابع بفرستم وتابع کارشو انجام بده.


$('#div1').click(function () {
$('#moreDesc1').slideToggle("fast");
});


$('#div2').click(function () {
$('#moreDesc2').slideToggle("fast");
});


$('#div3').click(function () {
$('#moreDesc3').slideToggle("fast");
});


$('#div4').click(function () {
$('#moreDesc4').slideToggle("fast");
});



با تشکر

Rejnev
چهارشنبه 13 آبان 1394, 19:58 عصر
ابتدا ساختار html ت رو کمی عوض کن:


<article class='article-item'>
<h2>this is title<h2>
<p class='desc'>this is some description about this article</p>
<a href='#'>more...</a>
<p class='full-article'>
this is full description about this article
this p tag is invisible at the first time you open the page....
</p>
</article>


<article class='article-item'>
<h2>this is title<h2>
<p class='desc'>this is some description about this article</p>
<a href='#'>more...</a>
<p class='full-article'>
this is full description about this article
this p tag is invisible at the first time you open the page....
</p>
</article>



حالا توی کد js به این صورت میتونی p با کلاس full-article رو نشون بدی:



$('article.article-item > a').on('click',function(){

$(this).parent().find('p.full-article').slideToggle();
return false;
});