...">

Show / hide no frame

Search for a script that can show / hide functions without a frame.

Sort of:

<span rel="toggle" href="/somelink.html">Title</span>
<div class="toggle">Hidden block</div>

.toggle { display: none; }

The block .toggleshould be displayed after clicking span. Like toggle()in jQuery.

Thank.

+3
source share
5 answers

look here to create the getElementByClass function - http://www.dustindiaz.com/getelementsbyclass/

then something like this (didn't check if it works, but you get the idea):

toggleItem = function(){
  var item = getElementByClass('toggle')[0];
  if (item.style.display == "block")
  {
    item.style.display = 'none';
  }
  else
  {
    item.style.display = 'block';
  }
}
+9
source

I would create two methods that add / remove a class togglewith native JavaScript:

function show(element) {
    element.className += " toggle";
}

function hide(element) {
    element.className = (element.className).replace(/\s*toggle/g, "");
}

, onclick . ?

0

, , div . , . W3C, , IE - , .

HTML:

<!-- the rel attribute in the span specifies which div to toggle -->
<span rel="target" id="trigger">Title</span>

<div id="target">Hidden block</div>

JavaScript, script HEAD .js:

window.addEventListener('load', init, false);

function init() {
    document.getElementById('trigger').addEventListener(
        'click',
        function() {
            targetId = this.getAttribute('rel');
            var element = document.getElementById(targetId);
            if (element.style.display == 'block')
                element.style.display = 'none';
            else
                element.style.display = 'block';
        },
        false
    );
}

HTML JS.

0

.

HTML

<span rel="hidden" href="/somelink.html" onclick="toggle(this)">Title</span>
<div class="toggle" id="hidden">Hidden block</div>

Javascript

function toggle(el) {
  if (!el.getAttribute("rel")) return;
  el = document.getElementById( el.getAttribute("rel"));
  var cname = " " + el.className + " ";
  if (cname.indexOf("toggle") !== -1) {
    cname = cname.replace(" toggle ", " ");
    el.className = cname.substring(1, cname.length-1);
  } else {
    el.className += " toggle";
  }
}
0

-, , , . , , . , id = 'some_id', , . toggle .

// returns a function that will toggle the given element
function makeToggleFunction(el) {
        var element = el;
        return function() {
                if (element.style.display == 'none')
                        element.style.display = 'block';        
                else
                        element.style.display = 'none';

        };
}

window.addEventListener('load', on_window_load, false);
var GLOBAL = {};
function on_window_load() {
        GLOBAL.toggle_element = makeToggleFunction(document.getElementById('some_id'));
}

, , GLOBAL.toggle_element().

, , IE, .

document.addEventListener("DOMContentLoaded", on_window_load, false);

EDIT:

( http://www.dustindiaz.com/getelementsbyclass/, programatique)

function getElementsByClass(searchClass,node,tag) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}

on_window_load:

GLOBAL.toggleable = new Array();
or each(var element in getElementsByClass('toggle')) {
        GLOBAL.toggleable.push(makeToggleFunction(element));
}
GLOBAL.toggle_all = function() {
        for each(var f in GLOBAL.toggleable) {
                f.call();
        }
};  

GLOBAL.toggle_all(), , toggle.

0
source

Source: https://habr.com/ru/post/1752856/


All Articles