简体   繁体   English

如何使用JavaScript在CSS中定位元素

[英]How to locate the element within CSS using javascript

The HTML elements' position are determined in a linked CSS. HTML元素的位置在链接的CSS中确定。

div.title  
{  
position:absolute;  
top:12px;  
}

I can only use document.styleSheets[0].cssRules[0].position to find the value of the position of the title, but how can I know this position belongs to the title? 我只能使用document.styleSheets[0].cssRules[0].position查找标题位置的值,但是我怎么知道这个位置属于标题呢?
In other word, how can the js code know the document.styleSheets[0].cssRules[0] is for the title? 换句话说,js代码如何知道document.styleSheets[0].cssRules[0]是标题?

There are a few browser differences when getting styles. 获取样式时,浏览器存在一些差异。 To get a style rule generically: 通用获取样式规则:

function getStyleRule(s) {
  var sheet, sheets = document.styleSheets;
  var ruleProp, rule, rules;

  for (var i=0, iLen=sheets.length; i<iLen; i++) {
    ruleProp = ruleProp || (sheets[i].cssRules? 'cssRules' : 'rules');
    rules = sheets[i][ruleProp];

    for (var j=0, jLen=rules.length; j<jLen; j++) {
      rule = rules[j];
      if (s == rule.selectorText) {
        return rule;
      }
    }
  }
}

If you just want to match the first rule of the first sheet, then: 如果您只想匹配第一张纸的第一条规则,则:

var sheet = document.styleSheets[0];
var rules = sheet.cssRules || sheet.rules;
alert( rules[0] );

and to see the selector: 并查看选择器:

alert( rules[0].selectorText );

and to see the styles that have been set: 并查看已设置的样式:

alert( rules[0].cssText || rules[0].style.cssText);

Noting that some browsers will return exactly what is written in the style sheet and others will return their interpretation of that (ie it is implementation dependent and can't be directly compared across browsers). 请注意,某些浏览器将完全返回样式表中编写的内容,而其他浏览器将返回其解释(即,它依赖于实现,并且不能在各个浏览器之间直接进行比较)。

I think this answers your question: 我认为这可以回答您的问题:

You can use document.styleSheets[0].cssRules[0].selectorText which will give you 'div.title' 您可以使用document.styleSheets[0].cssRules[0].selectorText来给您“ div.title”

you can check if the style is the one you want by iterating the stylesheets then the rules within (two loops) 您可以通过迭代样式表然后检查其中的规则(两个循环)来检查样式是否为所需样式

definitely not very efficient compared to getting computed styles instead, but here's an example 与获取计算样式相比,绝对不是很有效,但这是一个示例

// iterate stylesheets
var css = document.styleSheets;
for (var i in css) {
    if (!isNaN(i)) {
        // iterate rules in the stylesheets
        for (var j in css[i].cssRules) {
            if (!isNaN(j)) {
                var rule = css[i].cssRules[j];
                if (rule.selectorText.indexOf('div.title') > -1) {
                    // do something with position value
                    alert(css[i].cssRules[j].style.position);
                }
            }
        }
    }
}

jsfiddle jsfiddle

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

相关问题 使用CSS选择器查找存储在javascript元素中的某些数据 - Using a CSS Selector to locate some data stored in a javascript element 如何使用JavaScript定位并单击iFrame中的元素 - How to use javascript to locate and click an element in an iFrame 如何使用javascript获取元素的css选择器? - How to get css selector for an element using javascript? NoSuchElementError:没有这样的元素:无法使用 Selenium Javascript 定位元素 - NoSuchElementError: no such element: Unable to locate element using Selenium Javascript 如何使用 JavaScript 在嵌入式 CSS 中更改 CSS 样式规则 - How to change CSS style rule within an embedded CSS using JavaScript 尽管使用.switchTo()。defaultContent(),但无法在form标签内定位元素 - Unable to locate element within form tag despite using .switchTo().defaultContent() 如何使用嵌套的for循环在JavaScript中找到对象内数组的名称和列表 - How to locate the name, and list, of an array within an object in JavaScript using nested for loop Nightwatch错误:无法找到元素:使用:CSS选择器 - Nightwatch ERROR: Unable to locate element: using: css selector 如何使用innerHTML定位页面中的元素 - How to locate element in a page using innerHTML 如何使用 Puppeteer 解决和定位元素 - How to solve and locate an element using Puppeteer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM