Javascript - getElementById returning null despite elements with given IDs existing on page
I am writing a number of functions to show and hide various divs on a page by applying style classes called "hidden" and "visible" using setAttribute. This function is intended to hide several divs at once. The ID of each div to be given the class "hidden" is listed in an array.
Each div may have more than one class, so when a div is given the "hidden" class, it's other class(es) must be preserved, except for the "visible" class being replaced.
function hideSections() {
// Initialise array with div IDs
var divs = new Array("tab-1", "tab-2", "tab-3", "tab-4", "tab-5");
// Loop through divs in array
for (var count = 0; count < divs.length; count++) {
// Get existing classes
var div = document.getElementById(divs[count]);
var divClass = div.get开发者_如何转开发Attribute("class");
// Remove "visible" class if it exists
divClass = divClass.replace("visible", "");
// Append "hidden" class
div.setAttribute("class", divClass + " hidden");
}
}
For some reason this function is not working, though it is definitely being called.
An alert() placed inside the loop appears, if placed before the line [[var divClass = div.getAttribute("class");]]. Placed after this line, it does not, so I'm guessing this line is where the problem is.
All the divs have a class attribute specified.
My guess is you have elements which don't have a class attribute so divClass
is null - causing an error on line divClass = divClass.replace("visible", "");
. (can't call a method off a null value)
Try checking for the attribute:
// Initialise array with div IDs
var divs = new Array("tab-1", "tab-2", "tab-3", "tab-4", "tab-5");
// Loop through divs in array
for (var count = 0; count < divs.length; count++) {
// Get existing classes
var div = document.getElementById(divs[count]);
var divClass = '';
if(divClass = div.getAttribute("class")) {
// Remove "visible" class if it exists
divClass = divClass.replace("visible", "");
}
// Append "hidden" class
div.setAttribute("class", divClass + " hidden");
}
... or you can check out the JSFiddle demo I created.
What you need are utility functions to add and remove classes. Here's some I use:
var trim = function(s) {
return s.replace(/(^\s+)|(\s+$)/g,'').replace(/\s+/g,' ');
}
var hasClassName = function(el, cName) {
if (typeof el == 'string') el = document.getElementById(el);
var re = new RegExp('(^|\\s+)' + cName + '(\\s+|$)');
return el && re.test(el.className);
}
var addClassName = function(el, cName) {
if (typeof el == 'string') el = document.getElementById(el);
if (!hasClassName(el, cName)) {
el.className = trim(el.className + ' ' + cName);
}
}
var removeClassName = function(el, cName) {
if (typeof el == 'string') el = document.getElementById(el);
if (hasClassName(el, cName)) {
var re = new RegExp('(^|\\s+)' + cName + '(\\s+|$)','g');
el.className = trim(el.className.replace(re, ''));
}
}
Now you can do:
addClass(elementOrId, 'classValue');
Wrap in a "namespace" or whatever suits.
Try something like this:
function hideSections() {
// Initialise array with div IDs
var divs = ["tab-1", "tab-2", "tab-3", "tab-4", "tab-5"];
// Loop through divs in array
for (var count = 0; count < divs.length; count++) {
// Get existing classes
var div = document.getElementById(divs[count]);
var divClass = div.getAttribute("class");
// Remove "visible" class if it exists
var regex = new RegExp( '(?:^|\\s+)' + 'visible' + '(?=\\s|$)', 'i' );
if ( regex.test( divClass ) ) {
divClass = divClass.replace( regex, '' ).replace( /^\s+/, '' );
// Append "hidden" class
if ( divClass )
div.setAttribute( 'class', divClass + ' hidden' );
else
div.setAttribute( 'class', 'hidden' );
}
}
}
Also you can try this out from jsfiddle
精彩评论