请教关于css3选择器的问题

yiyamzen 2016-04-12 04:24:02
下面的代码,当你单击第一个radio时会显示第一个panel的内容,单击第二个radio时会显示第二个panel的内容。
但是当在<form method="post" action="#" class="form">后加入<input type="hidden">后单击第一个radio时会显示第二个panel的内容,加入的<input type="hidden">为什么会影响pannel的显示呢?


<style>
form.form .panel {
display: none;
background: #fff;
}
form.form .panel:first-child { display: block; }
form.form input[name="tabs"]:checked ~ .panels .panel {
display: none;
}
form.form input[name="tabs"]:nth-of-type(1):checked ~ .panels .panel:nth-child(1),
form.form input[name="tabs"]:nth-of-type(2):checked ~ .panels .panel:nth-child(2) {
display: block;
}
</style>
<div id="content">
<form method="post" action="#" class="form">
<!-- <input type="hidden"> -->
<input id="one" name="tabs" type="radio" ><label for="one">Tab One</label>
<input id="two" name="tabs" type="radio"><label for="two">Tab Two</label>

<div class="panels">
<div class="panel">11</div>
<div class="panel">22</div>
</div>
<input type="submit" value="Submit">
</form>
</div>
...全文
155 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
风中的少年 2016-04-13
  • 打赏
  • 举报
回复
2楼大神。。。。。其实我觉得还不如用js去控制。。。将它写到方法中,以方便后期的维护。 $('<input [name="tabs"]').each(function (index) { $(this).click(function () { $('.panel').eq(index).show(); }) })
yiyamzen 2016-04-13
  • 打赏
  • 举报
回复
引用 2 楼 haoyue70 的回复:
form.form input[name="tabs"]选择到了两个input标签, :nth-of-type(1)选择的是同级里input标签的第一个, 仅仅针对标签不能针对类名 属性名等。
非常感谢
yiyamzen 2016-04-13
  • 打赏
  • 举报
回复
引用 1 楼 jslang 的回复:
nth-of-type(n) 中的n是指在父元素中的第n个子元素, 你在前面加了一个元素 <input type="hidden"> 后面的元素在父元素中的排行就向下移了。 你要nth-of-type()中的数值 <style> form.form .panel { display: none; background: #fff; } form.form .panel:first-child { display: block; } form.form input[name="tabs"]:checked ~ .panels .panel { display: none; } form.form input[name="tabs"]:nth-of-type(2):checked ~ .panels .panel:nth-child(1), form.form input[name="tabs"]:nth-of-type(3):checked ~ .panels .panel:nth-child(2) { display: block; } </style> <div id="content"> <form method="post" action="#" class="form"> <input type="hidden"> <input id="one" name="tabs" type="radio" ><label for="one">Tab One</label> <input id="two" name="tabs" type="radio"><label for="two">Tab Two</label> <div class="panels"> <div class="panel">11</div> <div class="panel">22</div> </div> <input type="submit" value="Submit"> </form> </div>
谢谢
haoyue70 2016-04-12
  • 打赏
  • 举报
回复
form.form input[name="tabs"]选择到了两个input标签, :nth-of-type(1)选择的是同级里input标签的第一个, 仅仅针对标签不能针对类名 属性名等。
天际的海浪 2016-04-12
  • 打赏
  • 举报
回复
nth-of-type(n) 中的n是指在父元素中的第n个子元素, 你在前面加了一个元素 <input type="hidden"> 后面的元素在父元素中的排行就向下移了。 你要nth-of-type()中的数值 <style> form.form .panel { display: none; background: #fff; } form.form .panel:first-child { display: block; } form.form input[name="tabs"]:checked ~ .panels .panel { display: none; } form.form input[name="tabs"]:nth-of-type(2):checked ~ .panels .panel:nth-child(1), form.form input[name="tabs"]:nth-of-type(3):checked ~ .panels .panel:nth-child(2) { display: block; } </style> <div id="content"> <form method="post" action="#" class="form"> <input type="hidden"> <input id="one" name="tabs" type="radio" ><label for="one">Tab One</label> <input id="two" name="tabs" type="radio"><label for="two">Tab Two</label> <div class="panels"> <div class="panel">11</div> <div class="panel">22</div> </div> <input type="submit" value="Submit"> </form> </div>

61,112

社区成员

发帖
与我相关
我的任务
社区描述
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
社区管理员
  • HTML(CSS)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧