如何获得CSS样式的类

时间:2019-01-20 05:58:49

标签: javascript jquery css

我通过此代码获取元素的所有CSS样式

"background-image": "none",

到目前为止一切都很好。

现在我需要知道wich类是否将CSS样式添加到此列表中。

例如,我们用

查找结果行
import UIKit

struct TestData {
    let Label : String
    var Money : Double
}

var SaveDate = [TestData]()
SaveDate.append(TestData(Label: "test1", Money: 55))
SaveDate.append(TestData(Label: "test1", Money: 35))
SaveDate.append(TestData(Label: "test2" , Money: 15))
SaveDate.append(TestData(Label: "test1" , Money: 10))
SaveDate.append(TestData(Label: "test3" , Money: 30))

var dictionaryOfValues:[String:Double] = [:]

func addUp() {
    for currentItem in SaveDate {
        dictionaryOfValues[currentItem.Label] = (dictionaryOfValues[currentItem.Label] == nil ? currentItem.Money : dictionaryOfValues[currentItem.Label] + currentItem.Money)
    }
}

addUp()
print(dictionaryOfValues) //["test1": 100.0, "test2": 15.0, "test3": 30.0]

现在,我需要知道是否有一类如何发送此样式,以及是否有一个类,该名称的类具有此类,最好是从wich css文件中获取。

如果有一种方法可以做到这一点,那么信息是最好的方法?

非常感谢。

1 个答案:

答案 0 :(得分:0)

以下代码在我的本地主机中经过测试。我用过jQuery。

HTML                  

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" href="style2.css">
        <title>Document</title>
    </head>

    <body id="body">


        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
            jQuery(document).ready(function () {
                captureLinks();
            });

            function captureLinks() {
                hrefs = [];
                jQuery("link").each(function () {
                    hrefs.push(jQuery(this).attr('href'));
                })
                css_styles = {};
                hrefs_count = 0;
                jQuery.each(hrefs, function (index, href) {
                    jQuery.ajax({
                        url: href,
                        success: function (data) {
                            css_styles[href] = data.replace(/ /g, '').replace(/(\r\n\t|\n|\r\t)/gm, "");
                        },
                        complete: function () {
                            hrefs_count = hrefs_count + 1;
                            if (hrefs_count === hrefs.length)
                                allCssCaptured(css_styles);
                        }
                    })
                })
            }

            function allCssCaptured(css_styles) {
                css_reference = "background-image:none";
                css_reference_remove_white_space = css_reference.replace(/ /g, ':');
                css_sheet_reference = {};
                jQuery.each(css_styles, function (filename, content) {
                    if (content.indexOf(css_reference_remove_white_space) !== -1) {
                        split_content = content.split(css_reference_remove_white_space);
                        left_of_css_reference = split_content[0];
                        for (var i = left_of_css_reference.length; i >= 0; i--) {
                            if (left_of_css_reference[i] === '{') {
                                j = i - 1;
                                for (j = i - 1; j >= 0; j--) {
                                    if (left_of_css_reference[j] === ".") {
                                        css_string = '';
                                        for (var k = j; k < i; k++) {
                                            css_string += left_of_css_reference[k];
                                        }
                                        css_sheet_reference[filename] = css_string;
                                    }
                                }
                            }
                        }
                    }
                });
                console.log(css_sheet_reference)
            }
        </script>
    </body>

    </html>

样式表2

    .no-image {
        color:yellow;
        background-image: none;
    }

样式表

    .b{
        background-color: blue;
    }

    .a{
        background-color: #000;
    }


    .test{
        background-color: red;
        width: 100px;
        height: 50px;
    }

然后我得到了哪个样式表和哪个类的console.log结果。

谢谢。

enter image description here

相关问题