很多时候,我们想通过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. bash 脚本编程七 将命令输出保存到变量中(转载)

    转自:http://blog.csdn.net/csfreebird/article/details/7978699 `符号包含的命令执行完后,可以讲其输出结果保存到变量中 #!/bin/bash v ...

  2. clipse maven 项目 出现红色叹号 解决方法

    感叹号代表jar包不全,看你是maven工程,pom.xml报错,应该是jar包路径不对.打开pom.xml文件,查看报错位置和报错信息. 缺失的jar包的配置去http://search.maven ...

  3. hdoj5793 A Boring Question【找规律】

    找出的规律.... 1 2 3 2 2 7 3 2 15 4 2 31 5 2 63 1 3 4 2 3 13 3 3 40 4 3 121 5 3 361 然后我们来推个公式: 比如说a2=3a1+ ...

  4. sql server编写通用脚本自动检查两个不同服务器的新旧数据库的表结构差异

    问题:工作过程中,不管是什么项目,伴随着项目不断升级版本,对应的项目数据库业务版本也不断升级,数据库出现新增表.修改表.删除表.新增字段.修改字段.删除字段等变化,如果人工检查,数据库表和字段比较多的 ...

  5. python 容器 生成器 迭代器 总结

    一.容器 容器是一种把多个元素组织在一起的数据结构,容器中的元素可以逐个地迭代获取,可以用in, not in关键字判断元素是否包含在容器中.通常这类数据结构把所有的元素存储在内存中. >> ...

  6. poj 3734 Blocks【指数型生成函数】

    指数型生成函数,推一推可得: \[ (1+\frac{x^1}{1!}+\frac{x^2}{2!}+\frac{x^3}{3!}+...)^2+(1+\frac{x^2}{2!}+\frac{x^4 ...

  7. 2.while循环 编码的初识,逻辑运算符 格式化输出

    while循环 循环 while True: # while 是关键字 条件 print('精忠报国') print('团结就是力量') print('北京欢迎你') print('葫芦爷爷救娃娃') ...

  8. bzoj1179: [Apio2009]Atm 【缩点+spfa最长路】

    题目传送门 Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruser i 银行的 ATM 取款机.令人奇怪的是,S ...

  9. $ybt\ 【信息学奥赛一本通】题解目录$

    [信息学奥赛一本通]题解目录 $ \large -> OJ$ $ problem1000 $ \(Answer\) - > $ \large 1000$ $ problem1001 $ \ ...

  10. Hexo瞎折腾系列(1) - 准备工作与简单美化

    前言 网上有不少相关的帖子,不过版本会比较旧,而不同版本可能存在代码不同的问题,不过大部分还是大同小异,本系列就不啰嗦重复了,基本只会按照本人所使用的版本以及个人所使用到的内容来进行介绍. 该系列是对 ...