简体   繁体   English

css选择具有由外部样式表设置的样式规则的元素

[英]css select elements with style rule set by external style sheet

With css you can use 有了CSS,您可以使用

    [style="cursor:pointer"] {cursor:wait;}

but this only works if the style rule in set inline. 但这仅在内联设置样式规则时有效。 I want to do this when the style rule is set by an external style sheet. 当样式规则由外部样式表设置时,我想这样做。

here is a heavily commented codepen example. 是一个备受评论的Codepen示例。 Thanks guys. 多谢你们。

Edit: It is important to know that I can not edit the html or the external stylesheet(The first style rule in my example) I can only inject a stylesheet. 编辑:重要的是要知道我不能编辑html或外部样式表(本例中的第一个样式规则),我只能注入样式表。

Your code above works as expected, due to the fact that changes to elements through CSS don't actually 'manipulate' the elements themselves, but rather their appearance. 上面的代码可以按预期工作,因为通过CSS对元素所做的更改实际上并不“操纵”元素本身,而实际上是“操纵”元素的外观。 Essentially, CSS attributes only look at the DOM. 本质上,CSS属性仅查看DOM。 Let me break it down for you: 让我为您分解一下:

You have two elements on the page, both <input> . 您在页面上有两个元素,都是<input> The first input is styled inline with style="cursor:pointer" . 第一个输入的样式为style="cursor:pointer" You then have the following two components of CSS: 然后,您具有CSS的以下两个组件:

div [type="button"] {
  cursor: pointer;
}

[style="cursor:pointer"] {
  cursor: wait !important;
}

The first input skips over the first rule, as it is not inside a <div> . 第一个输入跳过第一个规则,因为它不在<div>内部。 it then has the second rule applied, as it is styled as a pointer cursor on page load (because it is inline). 然后它会应用第二条规则,因为它被设计为页面加载时的指针光标(因为它是内联的)。

The second element applies the first rule, as it is inside a <div> . 第二个元素应用第一个规则,因为它位于<div>内部。 You might expect that due to CSS reading from top-to-bottom, that it would apply the second rule as well. 您可能期望由于CSS从上到下的读取,它也将应用第二条规则。 This is not the case, as the CSS cursor attribute change is applied through a CSS rule , and is not present on page load. 情况并非如此,因为CSS cursor属性更改是通过CSS规则应用的 ,并且在页面加载时存在。 Thus, when the CSS file is loaded, it still reads it as a regular cursor, rather than a pointer cursor. 因此,当加载CSS文件时,它仍将其作为常规游标而不是指针游标读取。

If you change the second style to: 如果将第二种样式更改为:

input {
  cursor: wait !important;
}

You will see the second element correctly apply the wait cursor. 您将看到第二个元素正确地应用了wait光标。 This is because the second element was already an input when the CSS rules started to apply. 这是因为当CSS规则开始应用时,第二个元素已经是输入。

If you'd like to style a certain group of buttons with the wait cursor, while excluding other buttons, the best way to handle this is with a class : 如果您想使用wait光标为特定的一组按钮设置样式,而排除其他按钮,则最好的方法是使用class

.wait {
  cursor: wait;
}

<input type="button" class="wait">

Hope this helps! 希望这可以帮助! :) :)

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

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