[英]Toggle attribute selector CSS javascript
I want to toggle "fill: #000;" 我想切换“填充:#000;” from the CSS property "svg [id^='_']".
来自CSS属性“ svg [id ^ ='_']”。
Normally I'd use something like this, but I can't use this because "svg [id^='_']" isn't an element, right? 通常我会使用类似的东西,但是我不能使用它,因为“ svg [id ^ ='_']”不是元素,对吗?
pub.toggleClass = function(el, className) {
if (el.classList) {
el.classList.toggle(className);
} else {
var classes = el.className.split(' ');
var existingIndex = classes.indexOf(className);
if (existingIndex >= 0)
classes.splice(existingIndex, 1);
else
classes.push(className);
el.className = classes.join(' ');
}
};
When I try toggleClass it says "Uncaught TypeError: Cannot read property 'split' of undefined"... 当我尝试toggleClass时,它说“ Uncaught TypeError:无法读取未定义的属性'split'” ...
I've tried doing something like this: document.styleSheets[1].addRule("svg [id^='_']", 'fill: #000;');
我试过做这样的事情:
document.styleSheets[1].addRule("svg [id^='_']", 'fill: #000;');
but it isn't working cross-browser and it's not toggling... I say this because I get the feeling there is a simpler way to do this.. 但是它不能跨浏览器工作,也不能切换。我之所以这样说是因为我感到有一种更简单的方法可以执行此操作。
Update 1 更新1
I'm going to try this library: https://github.com/Box9/jss 我将尝试使用此库: https : //github.com/Box9/jss
I was able to use toggleClass by putting the attribute selector in the class I was adding. 通过将属性选择器放在要添加的类中,我可以使用toggleClass。 Rather than try to add the style onto the existing attribute selection, this was a lot simpler.
与其尝试将样式添加到现有属性选择中,不如将其简单得多。
.monotone [id^='_'] {
fill: #000;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.