Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery中如何更改背景颜色和文本颜色_Jquery - Fatal编程技术网

jquery中如何更改背景颜色和文本颜色

jquery中如何更改背景颜色和文本颜色,jquery,Jquery,如何更改段落部分的背景颜色和文本颜色?我使用了以下代码: $(document).ready(function(){ $("p:first-child").css("color", "red"); $(this).css("background", "black"); }); 现在我看到我的div文本颜色变为红色,但背景色不变。你为什么不这样写 $("p:first-child").css({"color":"red","background-color":"black"

如何更改段落部分的背景颜色和文本颜色?我使用了以下代码:

$(document).ready(function(){   
    $("p:first-child").css("color", "red");
    $(this).css("background", "black");
});

现在我看到我的div文本颜色变为红色,但背景色不变。

你为什么不这样写

$("p:first-child").css({"color":"red","background-color":"black"});
将属性作为对象传递给
.css()


您的代码不起作用,因为
$(此)
在document ready中只会指向文档,而不会指向其他元素。

您必须使用
背景色,而不是
背景色

$(document).ready(function(){   
    $("p:first-child").css("color", "red");
        $(this).css("background-color", "black"); // <----
});

但是为什么它不能与
$(此)
?谢谢Rajaprabhu,你的建议对我很有帮助。@ammarces
$(此)
将指向该上下文中的文档。这样更好@RajaprabhuAravindasamy,这不是声望问题,我只是想在投票前看到答案,谢谢;)@阿玛斯:)里面是什么?这个平台实际上是用来分享知识的。无论谁编辑我们的帖子。:)Loyalar先生,你是对的,但是为什么我添加了2次css,而反过来,相同的工作可以是1个css,像这样:$(“p:第一个孩子”).css({“颜色”:“红色”,“背景色”:“黑色”});我可以再问一个问题吗
$(document).ready(function(){   
    $("p:first-child").css("color", "red").css("background-color", "black");
});