简体   繁体   English

如何通过javascript或jquery修改css的selector *

[英]How to modify the selector * of css through javascript or jquery

I would like to know if it is possible to modify the "*" selector of css by means of javascript or jquery. 我想知道是否可以通过javascript或jquery修改css的“*”选择器。

For example, I have the following: 例如,我有以下内容:

* {
margin: 0px;
padding: 0px;
list-style: none;
text-decoration: none;
font-family: 'Roboto', sans-serif;
}

And I need to modify by means of javascript or jquery "font-family". 我需要通过javascript或jquery“font-family”进行修改。

Technically, * selects all elements. 从技术上讲,*选择所有元素。 So, all HTML elements should be in the body, right? 所以,所有HTML元素都应该在正文中,对吗? So, when you change it, change the whole body instead of all elements. 因此,当您更改它时,更改整个身体而不是所有元素。

$('body').CSS({'font-family':})

or 要么

$('*').CSS({'font-family':})

First of all you should select the elements of DOM. 首先,您应该选择DOM的元素。 Then we can do anything what we want by using javascript as follows. 然后我们可以通过使用javascript做任何我们想要的事情,如下所示。

getElementsByTagName("*") will return all elements from DOM. getElementsByTagName("*")将返回DOM中的所有元素。

var allElements = document.getElementsByTagName("*");
for (var i = 0, len = allElements.length; i < len; i++) {
    var element = allElements[i];
    // Change any style you want 
    // Ex- element.style.border = ...
}

Ref : https://www.w3schools.com/jsref/met_document_getelementsbytagname.asp 参考: https//www.w3schools.com/jsref/met_document_getelementsbytagname.asp

Just the same stuff your selector would be * ... 你的选择器就是* ...

So do $("*").css({"font-family": "Arial"}); 所以做$("*").css({"font-family": "Arial"}); if using jQuery... 如果使用jQuery ...

But pure JavaScript, you can do: 但是纯JavaScript,你可以这样做:

document.querySelectorAll("*").forEach(el => el.style.fontFamily = "Arial");

You could do the following in pure Javascript: 您可以在纯Javascript中执行以下操作:

 var everything = document.querySelectorAll("*");

 for(var i=0; i < everything.length; i++) {
     everything[i].style.fontFamily = "Arial";
     //add your CSS styles here
 }

This will loop through every element in the DOM and add the CSS. 这将循环遍历DOM中的每个元素并添加CSS。

Or if you are using Jquery, there is way easier method to do it. 或者,如果您使用的是Jquery,则可以采用更简单的方法。 See below 见下文

  $("*").css("font-family","Arial");

or just simply style the body 或者只是简单地塑造身体

$("body").css("font-family","Arial");

You can have access to your style sheets fom JS and change them directly (not by filter/change any html nodes) more info here . 您可以访问您的样式表FOM JS和直接改变他们(而不是由过滤器/更改任何HTML节点)更多信息在这里

 let rules=document.styleSheets[0].cssRules; let rule= [...rules].find(r=> r.selectorText=="*" ); rule.style.fontFamily="cursive"; 
 * { margin: 0px; padding: 0px; list-style: none; text-decoration: none; font-family: 'Roboto', sans-serif; } 
 Example test <div style="font-family: 'Roboto'">No font change</div> 

尝试这个

$("*").css({"font-family": "your-font"});

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

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