简体   繁体   English

使用JavaScript或jQuery更改CSS规则

[英]Changing CSS Rules using JavaScript or jQuery

Initial Research 初步研究

I am aware of using .css() to get and set the CSS rules of a particular element. 我知道使用.css()获取和设置特定元素的CSS规则。 I have seen a website with this CSS: 我看过一个使用此CSS的网站:

body, table td, select {
    font-family: Arial Unicode MS, Arial, sans-serif;
    font-size: small;
}

I never liked Arial Unicode as a font. 我从不喜欢Arial Unicode作为字体。 Well, that was my personal feel. 好吧,那是我的个人感觉。 So, I would use Chrome's Style Inspector to edit the Arial Unicode MS to Segoe UI or something which I like. 因此,我将使用Chrome的样式检查器Arial Unicode MS编辑为Segoe UI或我喜欢的东西。 Is there anyway, other than using the following to achieve the same? 无论如何,除了使用以下方法实现相同目的之外,还有吗?

Case I 案例一

$("body, table td, select").css("font-family", "Segoe UI");
  • Recursive, performance intensive. 递归,性能密集。
  • Doesn't work when things are loaded on the fly. 快速加载内容时不起作用。

Case II 案例二

$('<style>body, table td, select {font-famnily: "Segoe UI";}</style>')
    .appendTo("head");
  • Any other better method than this? 还有其他比这更好的方法吗?
  • Creates a lot of <style> tags! 创建很多<style>标签!

Ok, if: 好吧,如果:

  • Personal Preference 个人喜好

    Then use user styles CSS. 然后使用用户样式CSS。 According to priority, user styles takes precedence above all other styles. 根据优先级,用户样式优先于所有其他样式。 Next comes inline-styles, then !important styles, then specificity, then default browser styles. 接下来是内联样式,然后是!important样式,然后是特异性,然后是默认浏览器样式。 If it's just for personal preference, pack-up a custom CSS with you and a browser that supports it. 如果只是出于个人喜好,请与您以及支持该浏览器的浏览器一起打包自定义CSS。

  • You are the developer 你是开发商

    Then don't do this in JavaScript or user scripts. 然后,不要在JavaScript或用户脚本中执行此操作。 Go down to the problem and change those styles! 深入探讨问题并更改这些样式! You are just making the browser work more by actually making it parse stuff you don't want. 您实际上是通过使浏览器解析不需要的内容来使其工作更多。 Since you have access to the code, change the styles instead. 由于您可以访问代码,因此可以更改样式。

    However, since your site could be a public site, style it like genericly for everyone. 但是,由于您的网站可以是公共网站,因此通常适合所有人。 It's not only you that's viewing the site. 查看网站的不仅是您。

  • Not the developer: 不是开发商:

    The best way I can think of ( and already did this before* ) is to remove external stylesheets from the page and replace them with modded versions of your own liking. 我能想到的最好的方法( 并且以前已经做过* )是从页面中删除外部样式表,并用自己喜欢的修改版本替换它们。 That is taking a copy of the original CSS file, change what needs to be changed, and then load it via JS by creating a <link> to that external file, or load the contents via AJAX and put them in a <style> . 那就是获取原始CSS文件的副本,更改需要更改的内容,然后通过创建到该外部文件的<link>通过JS加载它,或者通过AJAX加载内容并将它们放在<style> However, this approach is riddled with obstacles. 但是,这种方法充满了障碍。 <link> does not have an onload event so you won't know the external CSS was loaded (there are crafty workarounds though) and AJAXing CSS contents imply that your CSS is in the same domain or the browser and server supports CORS. <link>没有onload事件,因此您将不知道外部CSS已加载(尽管有一些巧妙的解决方法),并且AJAXing CSS内容表示您的CSS位于同一域中,或者浏览器和服务器支持CORS。

    Another way you can do it is to have JS peek into loaded stylesheets and modify their contents. 您可以执行的另一种方法是让JS窥视已加载的样式表并修改其内容。 This is a more JS intensive work since JS looks for your definition to change in a pile of CSS declarations. 由于JS会在一堆CSS声明中查找要更改的定义,因此这是对JS的大量工作。 This is a safer bet, however, I believe not all browsers can traverse CSS styles or at least do it differently across browsers. 这是一个比较安全的选择,但是,我相信并非所有的浏览器都可以遍历CSS样式,或者至少在不同的浏览器之间可以做到这一点。 Also, if you got a large stylesheet, you'd be parsing it every time. 另外,如果您的样式表很大,那么您每次都会对其进行解析。

    As you said, .css() would be recursive. 如您所说, .css()将是递归的。 True, because it applies inline styles to each affected element. 正确,因为它将内联样式应用于每个受影响的元素。 This is good in a sense that inline styles are higher up in the priority so whatever is placed using .css() , you are almost sure that they will take effect. 从某种意义上说,这很不错 ,因为内联样式的优先级更高,因此无论使用.css()放置什么样式,您几乎都可以肯定它们会生效。 However, it's more work intensive since it involves the DOM now. 但是,由于现在涉及DOM,因此工作量更大。

* The library I created does not remove existing styles, it just loads and unloads predefined styles declared using it's API *我创建的库不会删除现有样式,它只是加载和卸载使用其API声明的预定义样式

Have a look at: 看一下:

I'm afraid there is no library for that, I really would like to see one... 恐怕没有图书馆了,我真的很想看看...

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

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