jQuery:根据包含元素的背景颜色更改文本颜色?
我想根据元素的背景颜色更改元素的文本颜色。
例如,如果背景颜色更改为黑色,则将文本设置为白色。如果背景颜色更改为白色,则将文本设置为黑色。
尽管用户可以将背景颜色更改为他们想要的任何颜色,但它会比这更复杂一些。
我发现了一些关于在后端处理它的随机文章,但没有使用javascript(或更好的是,jQuery)。
I want to change the text color of an element based on the element's background color.
For instance, if the background color is changed to black, then make the text white. If the background color is changed to white, then make the text black.
It'd be a bit more complex than than that though as the user can change the background color to whatever they'd like.
I found some random articles about handling it on the backend, but nothing using javascript (or better yet, jQuery).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,创建一个函数来确定颜色是否为深色(源,修改):
然后,使用以下内容:
http://jsfiddle.net/QkSva/
First, create a function that determines whether a color is dark or not (source, modified):
Then, use something along the lines of:
http://jsfiddle.net/QkSva/
我建议你使用 css 主题,因此更改为 css 文件。但你可以用 jQuery 来做到这一点。
这只是简单的解决方案,如果你真的想要这个,最好设置
背景变化的事件。
希望有帮助
I suggest you do use css theming, so change to css file, for that. But you can do it with jQuery.
this is only the simple solution, if you really want to this it would be good to set
a event on background changes.
hope that helps