简体   繁体   中英

How can I manipulate css into a SVG

i'm trying to improve my code by assigning the css for properly:

isChecked = jqElement.is(':checked');
type = jqElement.attr('tag');
var start = new Date().getTime();
elements = document.getElementsByClassName(type);
console.log('LAYER ' + type + ': ' + elements.length);
for (j=0; j<elements.length; j++){
    element = elements[j];
if (isChecked)
    element.classList.remove('hide-layer');
else
    element.classList.add('hide-layer');
//element.className += ' hide-layer';
//element.className = type;
//element.setAttribute('visibility', (isChecked)? '' : 'hidden');
}

Using element.className += ' hide-layer' doesn't work Using element.className += ' hide-layer' takes a while (8 secons for 1996 path elements with the same class)

the css class is like:

.hide-layer{
    visibility:hidden;
}

Hope you can help me. Thanks in advance!

If you set a class on the elements you wanted to hide you could manipulate the class rule ie edit

.hide-layer{
    visibility:hidden;
}

to become

.hide-layer{
    visibility:visible;
}

You can access stylesheets via

document.styleSheets

If the above rule was the only rule in the only stylesheet for the page it would be

var rule = document.styleSheets[0].cssRules[0]

and

document.styleSheets[0].cssRules[0].style.setProperty('visibility','visible',null);

would update the visibility property in the rule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM