简体   繁体   English

用jQuery将文本颜色更改为背景颜色

[英]Change text colour to background colour with jquery

Using Wordpress editor to change text colour, is there a way I can then use jquery to force it to make this the background colour instead. 使用Wordpress编辑器来更改文本颜色,有没有一种方法可以让我使用jquery强制使其变为背景颜色。

This is what wp renders when I change the colour of the text: 这是我更改文本颜色时wp呈现的内容:

<p>
   <span style="color: #ff0000;">
      <a style="color: #ff0000;" href="#">Link name</a>
   </span>
</p>

I want the background to be whatever colour I choose instead. 我希望背景变为我选择的任何颜色。

Something along the lines of, but I'm stuck. 有点类似,但是我被卡住了。 [THE SELECTED COLOUR] is just a placeholder so you can see what I'm trying to do. [THE SELECTED COLOUR]只是一个占位符,因此您可以看到我正在尝试做的事情。

$("p span a").css("background-color","[THE SELECTED COLOUR]");

How about this, where you iterate though them all 怎么样,您遍历所有这些

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <span style="color: #ff0000;"> <a style="color: #ff0000;" href="#">Link name</a> </span> </p> <p> <span style="color: #0000ff;"> <a style="color: #0000ff;" href="#">Link name</a> </span> </p> <script> $("p span a").each(function( index ) { $( this ).css("background-color", $( this ).css("color")); $( this ).css("color", "white"); }); </script> 

Or, as A.Wollf suggests in his comment, use a function 或者,如A.Wollf在其评论中建议的那样,使用一个function

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <span style="color: #ff0000;"> <a style="color: #ff0000;" href="#">Link name</a> </span> </p> <p> <span style="color: #0000ff;"> <a style="color: #0000ff;" href="#">Link name</a> </span> </p> <script> $("p span a").css({"background-color":function(){ return $(this).css('color'); }, "color": "white"}); </script> 

Hope this will be useful 希望这会有用

$("p span a").css("background-color","green");

DEMO 演示

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

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