简体   繁体   English

如何从javascript访问html的STYLE标记中的class属性?

[英]How can I access the class attribute in STYLE tag of html from javascript?

I have CSS in Style tag on the html header. 我在html标头上的Style标签中有CSS。 What I want is that thru javascript to access all the classes of Style tag and check whether it has font-weight:bold or not; 我想要的是通过javascript访问Style标记的所有类,并检查其是否具有font-weight:bold at the end I want an array of classes with font-weight:bold so that I can assign id attribute to them in javascript later. 最后,我想要一个带有font-weight:bold的类的数组,以便稍后可以在javascript中为它们分配id属性。

CSS CSS

span {
  white-space: pre-wrap;
}

.pt-Normal {
  line-height: 107.9%;
  margin-bottom: 8pt;
  font-family: Ca**strong text**libri;
  font-size: 20pt;
  margin-top: 0;
  margin-left: 0;
  margin-right: 0;
}

.pt-DefaultParagraphFont-000000 {
  font-family: Calibri;
  font-size: 11pt;
  font-style: normal;
  font-weight: bold;
  margin: 0;
  padding: 0;
}

.pt-DefaultParagraphFont-000001 {
  font-family: Calibri;
  font-size: 20pt;
  font-style: normal;
  font-weight: bold;
  margin: 0;
  padding: 0;
}

.pt-Normal-000002 {
  line-height: 107.9%;
  margin-bottom: 8pt;
  font-family: Calibri;
  font-size: 11pt;
  margin-top: 0;
  margin-left: 0;
  margin-right: 0;
}

.pt-DefaultParagraphFont-000003 {
  color: #FF0000;
  font-family: Calibri;
  font-size: 11pt;
  font-style: normal;
  font-weight: bold;
  margin: 0;
  padding: 0;
}

JAVASCRIPT from where I want to access the CSS class attribute. 我要从那里访问CSS类属性的JAVASCRIPT。

//this is just example of one class having fontWeight:bold, but i want to do this in a generic way.

function sec(){var s = document.getElementsByClassName("pt-DefaultParagraphFont-000001");
        for (var z = 0; z <= s.length;z++){s[z].setAttribute("id",z.toString());}}
    var sc = document.getElementsByTagName("STYLE").style;
        if (sc.fontWeight == bold){
        //here i want to get all the class which have class attribute fontWeight:bold
        //later on i want to assign id attribute to those elements which have fontWeight:bold
    }

use filter method it will retrun an array ok contains elements who have font-size: bold then just get the className by ok[index].className working example. 使用过滤器方法将重新运行一个数组ok包含具有font-size: bold元素font-size: bold然后通过ok[index].className工作示例获取className。

 var ok = $('[class]').filter(function() { return $(this).css('font-weight').toLowerCase().indexOf('bold') > -1; }); var arrayBold = [] for(var i=0;i<ok.length;i++){ arrayBold.push(ok[i].className); } console.log(arrayBold); 
 <script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="pt-Normal"></div> <div class="pt-DefaultParagraphFont-000000"></div> <div class="pt-DefaultParagraphFont-000001"></div> <div class="pt-Normal-000002"></div> <div class="pt-DefaultParagraphFont-000003"></div> <style> span { white-space: pre-wrap; } .pt-Normal { line-height: 107.9%; margin-bottom: 8pt; font-family: Ca**strong text**libri; font-size: 20pt; margin-top: 0; margin-left: 0; margin-right: 0; } .pt-DefaultParagraphFont-000000 { font-family: Calibri; font-size: 11pt; font-style: normal; font-weight: bold; margin: 0; padding: 0; } .pt-DefaultParagraphFont-000001 { font-family: Calibri; font-size: 20pt; font-style: normal; font-weight: bold; margin: 0; padding: 0; } .pt-Normal-000002 { line-height: 107.9%; margin-bottom: 8pt; font-family: Calibri; font-size: 11pt; margin-top: 0; margin-left: 0; margin-right: 0; } .pt-DefaultParagraphFont-000003 { color: #FF0000; font-family: Calibri; font-size: 11pt; font-style: normal; font-weight: bold; margin: 0; padding: 0; } </style> 

Use document.styleSheets to get the information you want. 使用document.styleSheets获取所需的信息。 This is an array which contains all the style sheets of the document. 这是一个包含文档的所有样式表的数组。 Each style sheet again has the cssRules array. 每个样式表都具有cssRules数组。 Each rule has the cssText property which caontains the information you want. 每个规则都具有cssText属性,该属性可发出您想要的信息。

document.styleSheets.forEach(function(styleSheet){

    styleSheet.cssRules.forEach(function(cssStyleRule) {

         console.log(cssStyleRule.cssText);   //Add your condition here and build the array
    })


})

I do not know if you are required to use Javascript but within jQuery this would be quite easy. 我不知道您是否需要使用Javascript,但是在jQuery中这将非常容易。 Then you only need this: 然后,您只需要这样:

$("*").each(function() {
    if ($(this).css("font-weight") == "bold") {
        $(this).attr("class", "bold");
    }
});

Which search through your CSS to find a DOM object that has your desired style: font-weight: bold; 通过CSS搜索以找到具有所需样式的DOM对象: font-weight: bold; and for every object that it found an extra CLASS* is been added trough using the .attr() which makes your life much easier I guess. 对于每个发现额外对象CLASS *的对象,都使用.attr()进行了添加,这使您的生活更加轻松。 If you want to add an ID to each DOM object be careful that this ID should be an unique. 如果要向每个DOM对象添加一个ID,请注意该ID应该是唯一的。 All this can be seen within this JSFIDDLE 所有这些都可以在此JSFIDDLE中看到


I recommend to you that you should set a class due to the fact that an ID should be unique and therefore you need to generate multiple id's. 我建议您设置一个类,因为一个ID应该是唯一的,因此您需要生成多个ID。 Which make selecting all those id's more difficult. 这使得选择所有这些ID变得更加困难。 (thanks @Kaiido for the tip) (感谢@Kaiido的提示)


In the end (in my created example) the output will be: 最后(在我创建的示例中)输出将是:

<body>
  <p class="class1 bold">
    SAMPLE TEXT 1
</p>
<p>
    SAMPLE TEXT 2
</p>
<p class="class2">
    SAMPLE TEXT 3
</p>
<p class="class3 bold">
    SAMPLE TEXT 4
</p>
</body>

Given this CSS: 鉴于此CSS:

.class1 {
    font-weight: bold;    
}

.class2 {
    font-weight: normal;    
}

.class3 {
    font-weight: bold;    
}

You can use cssRules of <style> element sheet property, iterate style properties, values within a for loop; 您可以使用<style>元素sheet属性的cssRules ,迭代style属性, for循环内的值; return selectorText of matched property, value 返回匹配属性的selectorText文本,值

 var style = document.querySelector("style") , rules = style.sheet.cssRules , matches = [] , match = ["fontWeight", "bold"]; for (var i = 0; i < rules.length; i++) { if (rules[i].style[match[0]] === match[1]) { matches.push(rules[i].selectorText) } } console.log(matches) 
 span { white-space: pre-wrap; } .pt-Normal { line-height: 107.9%; margin-bottom: 8pt; font-family: font-size: 20pt; margin-top: 0; margin-left: 0; margin-right: 0; } .pt-DefaultParagraphFont-000000 { font-family: Calibri; font-size: 11pt; font-style: normal; font-weight: bold; margin: 0; padding: 0; } .pt-DefaultParagraphFont-000001 { font-family: Calibri; font-size: 20pt; font-style: normal; font-weight: bold; margin: 0; padding: 0; } .pt-Normal-000002 { line-height: 107.9%; margin-bottom: 8pt; font-family: Calibri; font-size: 11pt; margin-top: 0; margin-left: 0; margin-right: 0; } .pt-DefaultParagraphFont-000003 { color: #FF0000; font-family: Calibri; font-size: 11pt; font-style: normal; font-weight: bold; margin: 0; padding: 0; } 

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

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