简体   繁体   English

如何检查SVG中是否缺少CSS类?

[英]How to check if css class is missing in SVG?

I'm using a dynamic class to show a list of svg icons. 我正在使用动态类来显示svg图标列表。 I need to display a sample image only if a css class ("icon") is missing. 仅当缺少CSS类(“ icon”)时,才需要显示示例图像。 How can I do this? 我怎样才能做到这一点?

     <svg class="icon${Id} icon${Id}-dims" preserveAspectRatio="xMidYMid meet"
 width="40px" height="40px"></svg>

Note: ID is a variable i use to make SVG classes dynamic. 注意:ID是用于使SVG类动态化的变量。 I need to know when there are no matching classes available in CSS. 我需要知道CSS中没有可用的匹配类。 I have around 4000 icon classes. 我大约有4000个图标类。

You can check like this also.. 您也可以像这样检查。

if(($('#test').attr('class')).indexOf("icon$") < 0) {


}

Here is the pure JS approach. 这是纯JS方法。

//check for a specific 'icon' class
if(!document.querySelector('svg').classList.contains(`icon${Id}`)){
    console.log(`class icon${Id} not found`);
}
//check if no class contains 'icon' in its name
if(document.querySelector('svg').className.indexOf('icon') === -1){
    console.log(`Can not find class which has the word 'icon' in its name`);
}

The above code uses ES6 strings template . 上面的代码使用ES6字符串模板

Try this: 尝试这个:

$st = $('svg').attr('class');
if ($st.slice(0,$st.indexOf(' ')) != $st.slice($st.indexOf(' ')+1,$st.indexOf('-'))) {
    alert('ch img')
//change img
}

https://jsfiddle.net/g9e3vcw3/2/ https://jsfiddle.net/g9e3vcw3/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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