简体   繁体   English

如何使用javascript更改网页上的字体颜色?

[英]How do I use javascript to change the font colour on my webpage?

I can't use JQuery sadly I need to use good old Javascript. 可悲的是,我不能使用JQuery,我需要使用良好的旧Javascript。

I have a forum and a black theme and when people use black text on it you can't see it. 我有一个论坛和一个黑色主题,当人们在其上使用黑色文字时,您看不到它。 So I want to use javascript to change all the black text on the page to white when the page loads. 因此,我想使用javascript在页面加载时将页面上的所有黑色文本更改为白色。

document.body.style.color = '#555555'

of course then you have to use the getElementById function instead of body if you want to select a specific element. 当然,如果要选择特定元素,则必须使用getElementById函数而不是body。

That's not really smart though. 不过,这并不是很聪明。 Go with CSS. 使用CSS。

Other answerers have posted JavaScript solutions if you really want to use JS for this, so I won't add another. 如果您真的想为此使用JS,其他答复者已经发布了JavaScript解决方案,因此我将不再添加其他解决方案。 However, I just thought I'd offer two alternatives: 但是,我只是想提供两种选择:

  • If you find that black is the default color for any posts, and you can modify your theme, don't use JavaScript for this — open your theme's CSS file, try to locate the style that makes post text black, and change it to white. 如果您发现黑色是所有帖子的默认颜色,并且您可以修改主题,请不要使用JavaScript-打开主题的CSS文件,尝试找到使帖子文本为黑色的样式,然后将其更改为白色。

  • If black text is simply caused by people changing the color to be such in their own posts, I think a better idea would be to just tell your members not to use black text. 如果黑色文本仅是由于人们在自己的帖子中将颜色更改为这种颜色而引起的,那么我认为一个更好的主意是只告诉您的成员不要使用黑色文本。 I can also see legitimate uses of black text on black backgrounds, eg for a lack of spoiler tags in your forum software. 我还可以看到在黑色背景上合法使用黑色文本,例如,由于论坛软件中缺少扰流标签。

如果您是想动态更改主题/样式,则可以使用javascript通过通过document.getElementsByTagName(“ link”)选择链接标签来禁用和启用头部的CSS链接元素。

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

相关问题 如何使用按钮更改网页上的字体颜色? - How to change font colour on webpage with a button? 如何更改此JavaScript代码的字体颜色? - How can I change the font colour of this JavaScript code? 如何连接我的功能checkKeyPressed以更改javascript中文本的颜色? - how do i connect my function checkKeyPressed to change the colour of my text in javascript? 如何使用 Javascript 一键更改 div 的颜色? - How do I change the colour of a div with one button using Javascript? 如何使用Javascript或jQuery重定向网页? JavaScript重定向 - How do I redirect my webpage with Javascript or jQuery ?? Javascript redirect 如何更改javascript计算的字体,颜色,大小等? - How can I change the font, colour, size etc. of a javascript calculation? 当我滚动到其他背景颜色时,如何使用CSS更改徽标的颜色 - How do I use CSS to alter the colour of my logo when I scroll onto a different background colour 如果使用javascript选中复选框,如何更改文本颜色和字体粗细? - How to change text colour and font weight if checkbox is checked with javascript? 如何为我的网页创建 function 以删除 JavaScript 中的项目? - How do I create a function that deletes an item in JavaScript for my webpage? 如何使用HTML和Javascript向网页添加元素? - How do I use HTML and Javascript to add elements to a webpage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM