问题
I'm a JS newbie trying to alter the following code. It looks like it's adding style attributes. I would like to add the class name "reveal" to this string or below. What would that syntax look like? Many thanks for your help, guys!
inline = ( elem.domEl.getAttribute( 'style' ) ) ? elem.domEl.getAttribute( 'style' ) +
'; visibility: visible; ' : 'visibility: visible; '
回答1:
(I guess) the HTML may look like this:
<div class="some other">
In such case, if you do it like
elem.domEl.className = 'reveal';
then you'll get
<div class="reveal">
All styles brought by class some and other are gone! You definitely don't want that, right? Also you don't want repeated classname, something like
<div class="reveal reveal">
So, here are the solutions:
The best way is to use HTML5 API classList
:
elem.domEl.classList.contains('reveal') // Check whether element has a classname
elem.domEl.classList.add('reveal') // Add a classname
elem.domEl.classList.remove('reveal') // Remove a classname
elem.domEl.classList.toggle('reveal') // Toggle a classname
If you still want to run the code in out-dated browsers, try these:
// Check whether element has a classname
function hasClass(ele, cls) {
var clsChecker = new RegExp("\\b" + cls + "\\b");
return clsChecker.test(ele.className);
}
// Add a classname
function addClass(ele, cls) {
var clsChecker = new RegExp("\\b" + cls + "\\b");
if(clsChecker.test(ele.className)) {
// ele already has the className, don't need to do anything
return;
}
ele.className += (' '+ cls);
}
// Remove a classname
function removeClass(ele, cls) {
var clsChecker = new RegExp("\\b" + cls + "\\b");
if(clsChecker.test(ele.className)) {
// ele does have the className, remove them all (in case repeated)
ele.className.split(clsChecker).join('');
}
}
// Toggle a classname
function toggleClass(ele, cls) {
var clsChecker = new RegExp("\\b" + cls + "\\b");
if(clsChecker.test(ele.className)) {
// ele does have the className, remove them all (in case repeated)
ele.className.split(clsChecker).join('');
} else {
// ele doesn't have the classname, add it
ele.className += (' '+ cls);
}
}
// Usages
hasClass(elem.domEl, 'reveal') // Check whether element has a classname
addClass(elem.domEl, 'reveal') // Add a classname
removeClass(elem.domEl, 'reveal') // Remove a classname
toggleClass(elem.domEl, 'reveal') // Toggle a classname
回答2:
The easiest way to do it would be to just modify the Element.className property. If by saying "add class" you mean to push back a class name (string) to the element regardless of the current class name (string), then this code would do the job.
elem.domEl.className = elem.domEl.className + ' ' + <YOUR_STRING_HERE>
回答3:
Use:
elem.domEl.setAttribute("class", "reveal");
回答4:
You need to write
inline = ( elem.domEl.getAttribute( 'style' ) ) ? elem.domEl.getAttribute( 'style' ) + '; visibility: visible; ' : 'visibility: visible; '
elem.domEl.className = "reveal";
The catch here is classname is not a style attribute, its an element property.
回答5:
May be you want to do like below:
inline = (elem.domEl.style.visibility == 'visible') ? 'hidden' : 'visible';
But if you just want to set the visible css then do this way:
elem.domEl.style.visibility = 'visible';
回答6:
You may try this:
inline = ( elem.domEl.getAttribute( 'class' ) ) ? elem.domEl.getAttribute( 'class' ) + ' classname ' : 'classname '
Also this is possible:
var cls = elem.domEl.className;
elem.domEl.className = cls ? cls + ' classname' : 'classname';
来源:https://stackoverflow.com/questions/27163587/how-do-i-add-a-class-name-to-javascript-string