很多时候,我们想通过html+css的方式实现排列在后方的代码在选中状态下,能控制排列在前的代码的样式。那么要怎么实现呢?在这里我就要用1个小技巧来完成。

  众所周知的,我们css中的选择器通常只能向下或者同级向下选中,而不能向上选中,这样就对于想控制前面样式的时候带来麻烦。input+label关联的方式即可实现,因为input和label是通过id值来进行关联的,而html中规定了,id值必须唯一,那么我将input放置在html的body下的任意位置,均可实现关联,所以为了实现后方代码控制前方代码,只需要将input放置到前方的代码之前就ok了。

  下面我们举一个例子:我要实现电影院选座的时候,点击下方的1人,即选中1个座位,点击2人  即选中2个座位的效果。

在这里我选择单选框(input的type类型为radio)。   同时将input的代码放置到main标签下的最开始地方,而label可以放置在后面任意位置,都可以通过id进行关联。这样我在点击‘1人’等按钮的时候,即可选中所需座位。

html代码如下:

 <main>
<div>
<p>激光3号厅银幕</p>
</div>
<!-- 推荐选座的input框 -->
<input name="select" type="radio" id="people1">
<input name="select" type="radio" id="people2">
<input name="select" type="radio" id="people3">
<input name="select" type="radio" id="people4">
<input name="select" type="radio" id="people5">
<!-- 选座区 -->
<section class="num"> <!-- 选座区座位 -->
<div class="num_r">
<!-- 一排 -->
<div class="num_r1">
<input type="checkbox" id="seat1_01">
<label for="seat1_01">
<i></i>
</label>
<input type="checkbox" id="seat1_02">
<label for="seat1_02">
<i></i>
</label>
<input type="checkbox" id="seat1_03">
<label for="seat1_03">
<i></i>
</label>
<input type="checkbox" id="seat1_04">
<label for="seat1_04">
<i></i>
</label>
<input type="checkbox" id="seat1_05">
<label for="seat1_05">
<i></i>
</label>
<input type="checkbox" id="seat1_06">
<label for="seat1_06">
<i></i>
</label>
<input type="checkbox" id="seat1_07">
<label for="seat1_07">
<i></i>
</label>
<input type="checkbox" id="seat1_08">
<label for="seat1_08">
<i></i>
</label>
<input type="checkbox" id="seat1_09">
<label for="seat1_09">
<i></i>
</label>
<input type="checkbox" id="seat1_10">
<label for="seat1_10">
<i></i>
</label>
<input type="checkbox" id="seat1_11">
<label for="seat1_11">
<i></i>
</label>
<input type="checkbox" id="seat1_12">
<label for="seat1_12">
<i></i>
</label>
<input type="checkbox" id="seat1_13">
<label for="seat1_13">
<i></i>
</label>
<input type="checkbox" id="seat1_14">
<label for="seat1_14">
<i></i>
</label>
<input type="checkbox" id="seat1_15">
<label for="seat1_15">
<i></i>
</label>
</div>
<!-- 二排 后面除了id值不一样以为,其他差不多,故省略-->
<div class="num_r2">
......
</div>
</div>
<p>银幕中央</p>
<!-- logo背景 -->
<img src="../IMG/logo.png" alt="" class="logo">
</section>
<!-- 推荐选座 -->
<section class="recommend">
<p>推荐</p>
<div>
<label for="people1">
<p>1人</p>
</label>
<label for="people2">
<p>2人</p>
</label>
<label for="people3">
<p>3人</p>
</label>
<label for="people4">
<p>4人</p>
</label>
<label for="people5">
<p>5人</p>
</label>
</div>
</section>
</main>

  css代码如下:

main input{
display: none;
}
input[id="people1"]:checked~section label[for="seat7_07"]>i{
background-image: url("../IMG/Screenshot_2016-10-21-17-14-02_07.png");
}
input[id="people2"]:checked~section label[for="seat7_07"]>i,
input[id="people2"]:checked~section label[for="seat7_08"]>i
{
background-image: url("../IMG/Screenshot_2016-10-21-17-14-02_07.png");
}
input[id="people3"]:checked~section label[for="seat7_07"]>i,
input[id="people3"]:checked~section label[for="seat7_08"]>i,
input[id="people3"]:checked~section label[for="seat7_09"]>i
{
background-image: url("../IMG/Screenshot_2016-10-21-17-14-02_07.png");
}
input[id="people4"]:checked~section label[for="seat7_07"]>i,
input[id="people4"]:checked~section label[for="seat7_08"]>i,
input[id="people4"]:checked~section label[for="seat7_09"]>i,
input[id="people4"]:checked~section label[for="seat7_06"]>i
{
background-image: url("../IMG/Screenshot_2016-10-21-17-14-02_07.png");
}
input[id="people5"]:checked~section label[for="seat7_07"]>i,
input[id="people5"]:checked~section label[for="seat7_08"]>i,
input[id="people5"]:checked~section label[for="seat7_09"]>i,
input[id="people5"]:checked~section label[for="seat7_06"]>i,
input[id="people5"]:checked~section label[for="seat7_05"]>i
{
background-image: url("../IMG/Screenshot_2016-10-21-17-14-02_07.png");
}

  

纯html+css中实现静态选座位效果技巧(input+label使用小技巧)的更多相关文章

  1. C#中??和?分别是什么意思? 在ASP.NET开发中一些单词的标准缩写 C#SESSION丢失问题的解决办法 在C#中INTERFACE与ABSTRACT CLASS的区别 SQL命令语句小技巧 JQUERY判断CHECKBOX是否选中三种方法 JS中!=、==、!==、===的用法和区别 在对象比较中,对象相等和对象一致分别指的是什么?

    C#中??和?分别是什么意思? 在C#中??和?分别是什么意思? 1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; ...

  2. css中transition的使用以及:before:after的使用(小样式)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. angular源码分析:angular中各种常用函数,比较省代码的各种小技巧

    angular的工具函数 在angular的API文档中,在最前面就是讲的就是angular的工具函数,下面列出来 angular.bind //用户将函数和对象绑定在一起,返回一个新的函数 angu ...

  4. 移动端css知识总结--字体,毛玻璃效果,input和disabled

    移动端字体使用: font-family: Helvetica,sans-serif;我看这也是天猫使用的 透过背景看其他元素模糊,自身元素不模糊:-webkit-backdrop-filter: s ...

  5. 将td中文字过长的部分变成省略号显示的小技巧

    首先设置表格的样式table-layout:"fixed"再设置表格的宽度(这步必须) 最后再设置td样式的三个必要属性 代码如下: text-overflow: ellipsis ...

  6. 谈谈css中的before和after

    css中的伪元素before和after,其实有很多小的妙用. 一.基础用法 w3c中的基础用法:用来给元素的内容前面(对应:before)或者后面(对应:after)插入新内容. <p> ...

  7. ZH奶酪:纯CSS自定义Html中Checkbox复选框样式

    原文链接:http://www.lrxin.com/archives-683.html 首先看下效果: 点击演示地址查看实例. 首先,需要添加一段CSS隐藏所有的Checkbox复选框,之后我们会改变 ...

  8. Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结

    上一篇 | 下一篇 Spring MVC程序中得到静态资源文件css,js,图片 文件的路径 问题总结 作者:轻舞肥羊 日期:2012-11-26 http://www.blogjava.net/fi ...

  9. 转 纯CSS设置Checkbox复选框控件的样式

    Checkbox复选框是一个可能每一个网站都在使用的HTML元素,但大多数人并不给它们设置样式,所以在绝大多数网站它们看起来是一样的.为什么不把你的网站中的Checkbox设置一个与众不同的样式,甚至 ...

随机推荐

  1. 模板 - 字符串 - Manacher

    求最长回文子串. #include<bits/stdc++.h> using namespace std; #define ll long long ; ]; ]; int Manache ...

  2. mui中一级联动

    <!doctype html><html> <head> <meta charset="utf-8"> <title>& ...

  3. Ubuntu 设置文件默认打开的应用

    右键单击该文件,然后点击属性,打开属性面板 然后进入open with的选项,选择应用后,点击 set as default

  4. hbase-shell + hbase的java api

    本博文的主要内容有 .HBase的单机模式(1节点)安装 .HBase的单机模式(1节点)的启动 .HBase的伪分布模式(1节点)安装   .HBase的伪分布模式(1节点)的启动    .HBas ...

  5. [LOJ6041雅礼集训2017]事情的相似度

    题解 \(SAM+set\)启发式合并+扫描线 首先可以发现题目要求的就是查询结尾在一段区间内的\(LCS\) 这个显然就是\(SAM\)的\(parent\)树上的\(step[LCA]\) 我们可 ...

  6. Educational Codeforces Round 24 D

    Alice and Bob got very bored during a long car trip so they decided to play a game. From the window ...

  7. April Fools Contest 2017 E

    Description Input The input consists of four lines, each line containing a single digit 0 or 1. Outp ...

  8. compose 函数实现

    总结componse函数实现过程 大致特点 参数均为函数, 返回值也是函数 第一函数接受参数, 其他函数接受的上一个函数的返回值 第一个函数的参数是多元的, 其他函数的一元的 自右向左执行 简单实现 ...

  9. Win7系统出现提示: “Windows已遇到关键问题,将在一分钟后自动重新启动。”

    1. 若用户在使用Win7系统时,遇到上述系统故障,建议重启电脑.等电脑开机自检一过,马上按键盘上的F8键,选择进入安全模式.在安全模式下,进行系统还原.其他的解决方法见下. 1.或者,在安全模式下, ...

  10. 泛型generic