2017-04-21 32 views
0

我试图改变一个文本框的背景色在这个页面jQuery Descendant code使用jQuery派生选择

这里就使用jQuery后代选择一种形式是我使用的表单代码。

<form><div>Picnic Sign Up Form</div><label for="name">Full name</label> 
<input name="fullname" id="fullname"> 
<fieldset> 
<label for="firstname">Firstname here</label> 
<input name="firstname" id="firstname"/> 
<label for="lastname">Lastname here</label> 
<input name="lastname" id="lastname"/></fieldset></form> 
$("form fieldset input").css("backgroundColor", "yellow"); 

当我这样做时,它不会将字段集中的输入变成黄色。我的代码有什么问题?

回答

0

不能看他们是否已经添加在你上面的代码,但如果没有:

脚本需要在标签

另外,CSS的背景色是:背景颜色

<script> 
    $("form fieldset input").css("background-color", "yellow"); 
</script> 

而当你使用Jquery时,你需要在你的脚本之前包含jquery库。

+0

我能弄清楚我做错了什么。 backgroundColor的css假设为背景色。另外,我把我的代码放在document.ready函数中。它在我做到这一点时起作用,但当它在此功能之外时不起作用。这被提及为jQuery网站上的最佳实践。它说你必须这样做,以确保jQuery代码在页面完全加载后激活。还有更多关于这个帖子http://stackoverflow.com/questions/2645344/functions-inside-or-outside-jquery-document-ready&http://learn.jquery.com/about-jquery/how- jQuery的作品 – user2363682