javascript - What is an alternative to using getElementByClass for hiding multiple elements? -
I have a page with some links that I would like to be able to toggle with two buttons. It works with a link using getElementById, but I need to toggle some of those groups. I started with it, but it failed to work. I've heard that GetElementByClass has worked with everything but IE, but I'm using Opera 11.5 and it still does not work. I've searched a bit, but I'm new to some JavaScript and most of the explanation is not understood. Does anyone help me with a simple option, or can I help me fix the problem I've made? This is a test page I was using.
& lt; Head & gt; & Lt; Script type = "text / javascript" & gt; Function hideNames () {document.getElementByClass ("WebName"). Style.display = "none"; } Function ShowName () {document.getElementByClass ("WebName"). Style.display = "inline"; } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; P class = "webname" & gt; WebName & lt; / P & gt; & Lt; P class = "webname" & gt; Exam & lt; / P & gt; & Lt; Input type = "button" onclick = "hideNames ()" value = "hide web name" /> & Lt; Input type = "button" onclick = "showNames ()" value = "show web name" /> & Lt; / Body & gt;
To use the appropriate code and take action on the return of your code The result from that function as an array should work fine except for very old browsers, which do not have getElementsByClassName (which are all versions of IE until IE 9). Instead the option can be used that are not efficient, but work by filtering through all the tags in the document. If you want only one element, then use , document.getElementById ("Idvalue") and work on an ID instead of a class name. getElementById is also widely supported in older browsers. Here's how your code can use class names:
& lt; Head & gt; & Lt; Script type = "text / javascript" & gt; Function hide name () {var list = document.getElementsByClassName ("webname"); (Var i = 0; i & lt; list.length; i ++) {list [i] .style.display = "none"; }} Function showNames () {var list = document.getElementsByClassName ("WebName"); (Var i = 0; i & lt; list.length; i ++) {list [i] .style.display = "block"; }} & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; P class = "webname" & gt; WebName & lt; / P & gt; & Lt; P class = "webname" & gt; Exam & lt; / P & gt; & Lt; Input type = "button" onclick = "hideNames ()" value = "hide web name" /> & Lt; Input type = "button" onclick = "showNames ()" value = "show web name" /> & Lt; / Body & gt; PS & lt; P & gt; Tags are displayed: Block, not Display: inline. For the old browsers, the best part is that using a pre-built selector engine that will solve all cross-browser problems for you I have used YUI, jQuery and Sizzle (Sijal YUI And there is a selector engine inside jQuery). All are free and very good.
If you want to be the original javascript, you can also get some code for your own implementation of getElementsByClassName. Here are some sources. And.
To show you how easy it is, the whole code is for this:.
HTML is:
& lt; P class = "webname" & gt; WebName & lt; / P & gt; & Lt; P class = "webname" & gt; Exam & lt; / P & gt; & Lt; Input id = "hide" type = "button" value = "hide web name" /> & Lt; Input id = "show" type = "button" value = "show web name" /> The code is:
$ (document) .ready (function () {$ ("# hide"). Click (function () {$ (".name"), hide ();}); $ ("# show") Click (function () {$ (".name"). Show ();});});
Comments
Post a Comment