Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Не открывать все сразу (https://javascript.ru/forum/jquery/3653-ne-otkryvat-vse-srazu.html)

CompModdd 12.05.2009 22:41

Не открывать все сразу
 
Что имеется:
$(document).ready(function(){
		$("span.thead.").click(function () {
			if ($("div.alt1").is(":hidden")) {
				$("div.alt1").slideDown("slow");
			} else {
				$("div.alt1").slideUp("slow");
			}
		});
	});

и
<span class="thead"> ЗАГОЛОВОК </span>
<div class="alt1" style="display: none;"> СОДЕРЖИМОЕ </div>


Проблема в том что если есть несколько таких блоков то при клике по одному открываются все сразу.
Подскажите пожалуйста как это можно исправить, так чтобы каждый блок открывался по отдельности.

x-yuri 13.05.2009 11:17

this в обработчике - ссылка на элемент, по которому кликнули
next переходит к следующему элементу

e1f 13.05.2009 13:40

Предлагаю так:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.container {border:1px solid red;width:100px;height:50px}
.thead {border:1px solid green}
.alt1 {border:1px solid blue}
</style>
<script type="text/javascript" src="jquery-1.2.6.js" ></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
    $("span.thead").click(function () {
        $(this).siblings('div.alt1').slideToggle("slow")
    });
});
//-->
</script>
</head>
<body>
<div class="container"><span class="thead">H E A D</span><div class="alt1">Bla bla bla...</div></div>
<div class="container"><span class="thead">H E A D</span><div class="alt1">Bla bla bla...</div></div>
<div class="container"><span class="thead">H E A D</span><div class="alt1">Bla bla bla...</div></div>
</body>
</html>

CompModdd 13.05.2009 16:40

e1f спасиба за ценный совет :)


Часовой пояс GMT +3, время: 13:48.