从样式标签获取CSS类列表

时间:2018-07-10 06:49:20

标签: jquery html css asp.net

我想获取样式标签中使用的类的列表,并在以后附加到下拉列表中。我的ASPX页面中有以下代码

<style id="pageStyleCss" type="text/css">
  body {
    color: black;
  }
  
  .testclass1 {
    height: 100px;
  }
  
  .testclass2 {
    height: 100px;
    color: red;
  }
  
  #testid {
    color: black;
  }
</style>

with below code i can get rules list.
but client entered any wrong tags its difficult.
Hence please help me out if you know any pluggins do the same functionality.

   var resultarray= [];

var a = $('#pageStyleCss').html();

while (a.indexOf('{') != -1) {

resultarray.push(a.substring(0, a.indexOf('{')));

a = a.substring(a.indexOf('}') + 1);
}

The output I want is:
Class list :  testclass1,testclass2 

1 个答案:

答案 0 :(得分:0)

Below code helped me getting my requirement.   

  var resultarray = [];
        var a = $('#pageStyleCss').html();
        if (a != undefined) {
            while (a.indexOf('{') != -1) {
                resultarray.push(a.substring(0, a.indexOf('{')));
                a = a.substring(a.indexOf('}') + 1);
            }
        }
        var i, option = "<option value=''></option>"
        for (i = 0; i < resultarray.length; ++i) {
            if (resultarray[i].indexOf('.') > -1) {
                option += '<option value="' + resultarray[i].trim().replace('.', '') + '">' + resultarray[i].trim().replace('.', '') + '</option>';
            }
        }