<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
	<link rel="self" href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=10728&amp;type=atom" />
	<updated>2015-06-26T16:20:00Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.script-coding.com/viewtopic.php?id=10728</id>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94762#p94762" />
			<content type="html"><![CDATA[<p>Сколько угодно: текущая задача, многоуровневые меню и списки, слайдеры, &quot;карусели&quot;.</p>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2015-06-26T16:20:00Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94762#p94762</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94761#p94761" />
			<content type="html"><![CDATA[<p>Спасибо, этому нужно применение <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" /></p>]]></content>
			<author>
				<name><![CDATA[DD]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=25263</uri>
			</author>
			<updated>2015-06-26T16:06:57Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94761#p94761</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94760#p94760" />
			<content type="html"><![CDATA[<div class="codebox"><pre><code>
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;

.container {
    border: 1px solid #000;
    width: 300px;
}

.content {
    background-color: #ccc;
}

.hide    {
    display: none;
}

&lt;/style&gt;

&lt;script&gt;

function class2regex(name)
{
    return new RegExp(&#039;(?:^|\\s+)&#039; + name + &#039;(?=\\s+|$)&#039;, &#039;g&#039;);
};

function hasClass(object, name)
{
    var re = class2regex(name);
    return object.className.match(re);
};

function addClass(object, name)
{
    if ( hasClass(object, name) ) {
        return;
    }
    object.className = object.className + &#039; &#039; + name;
};

function removeClass(object, name)
{
    var re = class2regex(name);
    object.className = object.className.replace(re, &#039; &#039;);
};

function toggleClass(object, name)
{
    if ( hasClass(object, name) ) {
        removeClass(object, name);
    } else {
        addClass(object, name);
    }
};

// http://javascript.ru/unsorted/top-10-functions#8-getelementsbyclass
if ( document.getElementsByClassName ) {

    getElementsByClass = function(classList, node) {
        return (node || document).getElementsByClassName(classList);
    };

} else {

    getElementsByClass = function(classList, node) {
        var node = node || document,
        list = node.getElementsByTagName(&#039;*&#039;), 
        length = list.length,  
        classArray = classList.split(/\s+/), 
        classes = classArray.length, 
        result = [], i,j
        for(i = 0; i &lt; length; i++) {
            for(j = 0; j &lt; classes; j++)  {
                if(list[i].className.search(&#039;\\b&#039; + classArray[j] + &#039;\\b&#039;) != -1) {
                    result.push(list[i])
                    break;
                }
            }
        }
        return result;
    };

}

window.onload = function()
{
    var containers = getElementsByClass(&#039;container&#039;);
    for (var i = 0; i &lt; containers.length; i++) {
        containers[i].onclick = function()
        {
            var content = this.getElementsByTagName(&#039;div&#039;)[1];
            toggleClass(content, &#039;hide&#039;);
        };
    }
};

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;clicker&quot;&gt;CLICK ME&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;Lorem ipsum&lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;clicker&quot;&gt;CLICK ME&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;Lorem ipsum&lt;/div&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre></div><p>Обратите внимание, идентификаторы намеренно убрал, одни только классы. Используется функция getElementsByClass для кроссбраузерного получения списка объектов по имени класса.</p>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2015-06-26T15:56:28Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94760#p94760</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94759#p94759" />
			<content type="html"><![CDATA[<p><strong>Rumata</strong>, а если нужно будет увеличить количество блоков с идентификатором &quot;container&quot; в предложенном Вами коде, тогда как быть?</p><p><strong>wisgest</strong>, спасибо: сессионные куки, пожалуй, это заманчиво)) Покумекаю.</p>]]></content>
			<author>
				<name><![CDATA[DD]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=25263</uri>
			</author>
			<updated>2015-06-26T15:07:01Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94759#p94759</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94758#p94758" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>wisgest пишет:</cite><blockquote><p>Для обмена данными между страницами, загружаемыми в одно и то же окно, можно воспользоваться свойством window.name (если что, это строка).</p></blockquote></div><div class="quotebox"><cite>DD пишет:</cite><blockquote><p>А в CHM это работает?</p></blockquote></div><p>Легко проверить. Выбраем в меню окна HH.EXE пункт «Перейти по адресу (URL)...» и вводим </p><div class="codebox"><pre><code>javascript:void (window.name=&quot;Запомненное значение&quot;);</code></pre></div><p>переключаем показываемую страницу (переходя по ссылкам или через содержание) и вводим </p><div class="codebox"><pre><code>javascript:alert(window.name);</code></pre></div><div class="quotebox"><cite>DD пишет:</cite><blockquote><p>Впрочем, и назначение стиля по ID подошло бы.</p></blockquote></div><p>Без назначения стиля по ID или изменения правил CSS в любом случае (и с куками) не обойтись. Куки помогут лишь запоминанить какие-то значения, которые всё-равно надо как-то обработать. window.name действует как сессионные куки (т.е. до закрытия окна) и только для одного и того же окна.</p>]]></content>
			<author>
				<name><![CDATA[wisgest]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=3850</uri>
			</author>
			<updated>2015-06-26T14:31:50Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94758#p94758</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94757#p94757" />
			<content type="html"><![CDATA[<p>На будущее. предложенное решение со сменой атрибута стиля не самое лучшее. Например, оно работает для блочных элементов, но не работает для текстовых - они становятся блочными. Лучше делать через добавление удаление классого стиля, например как в следующем примере. Кода становится чуть больше, но использование его универсальнее и гораздо проще поддерживать другие решения на его основе. </p><p>Технология предполагает наличие классового стиля <strong>.hide</strong> (это пример, у вас может быть любой другой). Код добавляет и удаляет данный стиль в заданных элементах. </p><div class="codebox"><pre><code>
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;

.container {
    border: 1px solid #000;
    width: 300px;
}

.content {
    background-color: #ccc;
}

.hide    {
    display: none;
}

&lt;/style&gt;

&lt;script&gt;

function class2regex(name)
{
    return new RegExp(&#039;(?:^|\\s+)&#039; + name + &#039;(?=\\s+|$)&#039;, &#039;g&#039;);
};

function hasClass(object, name)
{
    var re = class2regex(name);
    return object.className.match(re);
};

function addClass(object, name)
{
    if ( hasClass(object, name) ) {
        return;
    }
    object.className = object.className + &#039; &#039; + name;
};

function removeClass(object, name)
{
    var re = class2regex(name);
    object.className = object.className.replace(re, &#039; &#039;)
};

function toggleClass(object, name)
{
    if ( hasClass(object, name) ) {
        removeClass(object, name);
    } else {
        addClass(object, name);
    }
};

window.onload = function()
{
    document.getElementById(&#039;container&#039;).onclick = function()
    {
        var content = this.getElementsByTagName(&#039;div&#039;)[1];
        toggleClass(content, &#039;hide&#039;);
    };
};

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div id=&quot;container&quot; class=&quot;container&quot;&gt;
&lt;div class=&quot;clicker&quot;&gt;CLICK ME&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;Lorem ipsum&lt;/div&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2015-06-26T13:39:19Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94757#p94757</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94755#p94755" />
			<content type="html"><![CDATA[<p>Спасибо еще раз! На всякий случай прикреплю то что получилось.</p><p><a href="http://forum.script-coding.com/misc.php?action=pun_attachment&amp;item=1002&amp;download=1&amp;secure_str=25263t10728">d.rar</a></p>]]></content>
			<author>
				<name><![CDATA[DD]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=25263</uri>
			</author>
			<updated>2015-06-26T11:56:04Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94755#p94755</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94754#p94754" />
			<content type="html"><![CDATA[<p>Если куки с локальными файлами не работают, то просто - никак.</p>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2015-06-26T11:28:49Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94754#p94754</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94753#p94753" />
			<content type="html"><![CDATA[<p>Спасибо большое, <strong>Rumata</strong>! Отлично работает!<br />Ещё хочется спросить: действительно ли без использования кук, иначе запоминание раскрытого спойлера из блока не сделать?</p>]]></content>
			<author>
				<name><![CDATA[DD]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=25263</uri>
			</author>
			<updated>2015-06-26T11:23:08Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94753#p94753</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94752#p94752" />
			<content type="html"><![CDATA[<p>Наколеночное решение. Но уже работает. Надо еще надо шлифовать. </p><p><strong>1.html</strong><br /></p><div class="codebox"><pre><code>
&lt;html&gt;
&lt;body&gt;

&lt;a href=&quot;2.html#id1&quot;&gt;block 1&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;2.html#id2&quot;&gt;block 2&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;2.html#id3&quot;&gt;block 3&lt;/a&gt;&lt;br /&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre></div><p><strong>2.html</strong><br /></p><div class="codebox"><pre><code>
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;

.content    {
    display: none;
}

&lt;/style&gt;
&lt;script&gt;

function toggleContent(block)
{
    var content = block.getElementsByTagName(&#039;div&#039;)[0];
    if ( ! content.className.match(/\bcontent\b/) ) {
        return;
    }

    if ( content.style.display == &#039;block&#039; ) {
        content.style.display = &#039;none&#039;;
    } else {
        content.style.display = &#039;block&#039;;
    }
};

window.onload = function()
{
    var id = window.location.hash;
    if ( ! id ) {
        return;
    }

    id = id.replace(/#/, &#039;&#039;);

    var block = document.getElementById(id);
    if ( ! block ) {
        return;
    }

    toggleContent(block);
};

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div id=&quot;id1&quot;&gt;BLOCK 1
&lt;div class=&quot;content&quot;&gt;
To be or not to be
&lt;/div&gt;
&lt;/div&gt;

&lt;div id=&quot;id2&quot;&gt;BLOCK 2
&lt;div class=&quot;content&quot;&gt;
Lorem ipsum
&lt;/div&gt;
&lt;/div&gt;
</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2015-06-26T10:01:40Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94752#p94752</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94751#p94751" />
			<content type="html"><![CDATA[<p>На целевой странице javascript-ом делаете проверку на наличие hash и открываете соответвующий div-блок (или какой-то другой). Этот блок должен существовать. При открытии страницы все блоки раскрывающиеся блоки должны быть закрыты (или их надо закрыть сразу после перехода на соответствующую страницу). Надо сконструировать страницу так чтобы, каждому раскрывающемуся блоку соответствовал ской hash в адресе страницы.</p>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2015-06-26T09:59:53Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94751#p94751</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94749#p94749" />
			<content type="html"><![CDATA[<p>А в CHM это работает? Впрочем, и назначение стиля по ID подошло бы.</p>]]></content>
			<author>
				<name><![CDATA[DD]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=25263</uri>
			</author>
			<updated>2015-06-26T08:23:42Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94749#p94749</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94746#p94746" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>DD пишет:</cite><blockquote><p>С куками можно было бы запоминать раскрытые спойлеры из определенных блоков при переходах. Что было бы гибче — потому что, например, при переходе на страницу назад, спойлер из соответствующего блока был бы раскрыт</p></blockquote></div><p>Для обмена данными между страницами, загружаемыми в одно и то же окно, можно воспользоваться свойством <em>window.name</em> (если что, это строка).</p>]]></content>
			<author>
				<name><![CDATA[wisgest]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=3850</uri>
			</author>
			<updated>2015-06-26T00:55:32Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94746#p94746</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94745#p94745" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>shiz пишет:</cite><blockquote><p>К чему упомянуты куки?</p></blockquote></div><p>С куками можно было бы запоминать раскрытые спойлеры из определенных блоков при переходах. Что было бы гибче — потому что, например, при переходе на страницу назад, спойлер из соответствующего блока был бы раскрыт. А здесь речь идёт как бы о полу-мере))</p><div class="quotebox"><blockquote><p>Hash знаю, что такое &quot;nash&quot;?</p></blockquote></div><p>Может опечатка была?</p><div class="quotebox"><blockquote><p>Только я в коде страниц (001.html, 002.html) не нашёл элементов с такими id (&quot;blok_1_1&quot;, &quot;blok_1_2&quot;...), к которым мог бы быть применён этот код.</p></blockquote></div><p>А так? —<br /></p><div class="codebox"><pre><code>
&lt;div id=&quot;d1&quot;&gt;data&lt;/div&gt;
&lt;div id=&quot;d2&quot;&gt;data&lt;/div&gt;
&lt;div id=&quot;d3&quot;&gt;data&lt;/div&gt;
</code></pre></div><p>Собственно, ID можно любые подставить.</p>]]></content>
			<author>
				<name><![CDATA[DD]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=25263</uri>
			</author>
			<updated>2015-06-25T23:54:56Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94745#p94745</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: JS: Разворот соответствующих блоков следующей страницы — при переходе]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=94744#p94744" />
			<content type="html"><![CDATA[<div class="quotebox"><blockquote><p>можно ли в CHM-файлах (в которых, как я недавно выяснил, куки не работают)</p></blockquote></div><p>К чему упомянуты куки?<br /></p><div class="quotebox"><blockquote><p>Подсказывали, что уместно воспользоваться hash, nash</p></blockquote></div><p>Hash знаю, что такое &quot;nash&quot;?<br /></p><div class="quotebox"><blockquote><p>может и этому есть применение?</p></blockquote></div><p>Только я в коде страниц (001.html, 002.html) не нашёл элементов с такими id (&quot;blok_1_1&quot;, &quot;blok_1_2&quot;...), к которым мог бы быть применён этот код.</p>]]></content>
			<author>
				<name><![CDATA[shiz]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=26249</uri>
			</author>
			<updated>2015-06-25T22:46:16Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=94744#p94744</id>
		</entry>
</feed>
