如何查看css中存在的类?

时间:2016-01-11 07:40:50

标签: jquery

我在css中有.hello课程:

<style>
    .hello { color:#ccc }
</style>

如何使用Jquery检查样式中是否存在类.hello
当然,即使在<link href='style.css' />文档中也需要检查所有样式。

4 个答案:

答案 0 :(得分:1)

首先指定要搜索现有类的外部文件,然后如果发现字符串“ gap”大于0,然后找到类“ gap”,则尝试将空格替换为“”。

  (function($){
            jQuery.get('assets/css/main.css', function(data) {
                var str = data.replace('n','');
                if(str.match(/gap/g).length>0) {
                    console.log('class was found!');
                }else{
                    console.log('no class!');
                }
            });       
        }) (jQuery);

答案 1 :(得分:1)

您可以使用getComputedStyle()作为元素来获取样式。

颜色以rgba的形式获得,并使用逻辑here

转换为十六进制

$('.hello').each((i, el) => {
  if (rgb2hex(getComputedStyle(el).color) == "#cccccc") {
    console.log('#cccccc el => ', el);
  }
});

function rgb2hex(rgb) {
  rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

function hex(x) {
let hexDigits = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");
  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
.hello {
  color: #cccccc;
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

<div class="hello">
  hello
</div>

<div class="hi">
  hi
</div>

答案 2 :(得分:0)

以下将检查是否将某些样式应用于元素(并不是绝对确认它是否来自样式表)

if ($('.hello').css('color') == '#CCC') {
   // true
} else {
   // false
}

答案 3 :(得分:0)

参见下面的代码片段,该函数从样式表或我们传递的样式标记返回找到的类或id。如果找不到,则返回一个空字符串。

Option Explicit

Sub PlotMultiSeries()

Dim i           As Long
Dim names1      As String
Dim names2      As String
Dim names3      As String
Dim MyCht       As Shape
Dim Ser         As Series

' axccording to your code, you are using "Sheet3" , fully qualify all your Ranges and Cells
With Worksheets("Sheet3")

    ' set the created Chart to a Shape variable
    Set MyCht = .Shapes.AddChart2(Style:=240, XlChartType:=xlXYScatterSmooth)

    For i = 1 To 1 ' your i loop is from 1 To 1 ???
        names1 = .Cells(2, i + 1).Address(False, False, xlA1, True)
        names2 = .Range(.Cells(3, i), .Cells(85, i)).Address(False, False, xlA1, True)
        names3 = .Range(.Cells(3, i + 1), .Cells(85, i + 1)).Address(False, False, xlA1, True)

        With MyCht
            ' add a new series
            Set Ser = .Chart.SeriesCollection.NewSeries
            ' define new Series properties
            With Ser
                .Name = "=" & names1
                .XValues = "=" & names2
                .Values = "=" & names3
            End With
        End With
    Next i
End With  

让我知道它是否适合你。