奇数行背景色:#FFF38F;偶数行背景色:#FFFFEE;选中行:#FF6500
选中行时,设置单选框选中状态
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./jquery.min.js"></script> <style> table { width: 320px; border: 1px solid black; text-align: center; border-collapse: collapse; } th, td { border: 1px solid black; padding: 5px; } .odd { background-color: #FFF38F; } .even { background-color: #FFFFEE; } .choose { background-color: #FF6500; } </style> </head> <body> <table> <thead> <th></th> <th>姓名</th> <th>性别</th> <th>暂住地</th> </thead> <tbody> <tr> <td><input type="checkbox" name="" id=""></td> <td>张三</td> <td>男</td> <td>四川成都</td> </tr> <tr> <td><input type="checkbox" name="" id=""></td> <td>张三</td> <td>男</td> <td>四川成都</td> </tr> <tr> <td><input type="checkbox" name="" id=""></td> <td>张三</td> <td>男</td> <td>四川成都</td> </tr> <tr> <td><input type="checkbox" name="" id=""></td> <td>张三</td> <td>男</td> <td>四川成都</td> </tr> <tr> <td><input type="checkbox" name="" id=""></td> <td>张三</td> <td>男</td> <td>四川成都</td> </tr> </tbody> </table> <script> $(function () { $('tbody>tr:odd').addClass('odd') $('tbody>tr:even').addClass('even') $('tbody tr').click(function () { $(this).addClass('choose') $(this).siblings().removeClass('choose') $(this).children('td').children('input').prop('checked', true) }) }) </script> </body> </html>
- Jquery中提供了filter方法进行过滤,如$("选择器").filter(内容过滤器)
- 搜索过程中可先隐藏hide所有数据行,满足条件的行进行显示show()
- :contains(text) 匹配包含给定文本的元素
- :empty 匹配所有不包含子元素或者文本的空元素
- :parent 匹配含有子元素或者文本的元素
- :has(selector) 匹配含有选择器所匹配的元素的元素
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./jquery.min.js"></script> <style> * { padding: 0; margin: 0; } body { margin: 100px; } table { width: 400px; border-collapse: collapse; border: 1px solid black; margin-top: 50px; text-align: center; } th, td { border: 1px solid black; } th { height: 30px; } </style> </head> <body> 查询:<input type="text"> <table> <thead> <th>姓名</th> <th>性别</th> <th>暂住地</th> </thead> <tbody> <tr> <td class="name">李四</td> <td>男</td> <td>四川成都</td> </tr> <tr> <td class="name">张三</td> <td>男</td> <td>四川成都</td> </tr> <tr> <td class="name">张三</td> <td>男</td> <td>四川成都</td> </tr> <tr> <td class="name">张三</td> <td>男</td> <td>四川成都</td> </tr> <tr> <td class="name">张三</td> <td>男</td> <td>四川成都</td> </tr> <tr> <td class="name">张三</td> <td>男</td> <td>四川成都</td> </tr> </tbody> </table> <script> $(function () { $('tbody>tr:even').css('backgroundColor', '#FFF38F') $('tbody>tr:odd').css('backgroundColor', '#FFFFEE') $('input').on('input', function () { $('.name').parent().hide() let txt = $('input').val() $('.name').filter(`:contains(${txt})`).parent().show() }) }) </script> </body> </html>
级联选择框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./jquery.min.js"></script> </head> <body> <select id="province"> <option>请选择</option> </select> <select id="cities"> <option>请选择</option> </select> <script> $(function () { const prov = ['河北省', '辽宁省', '四川省'] const city = [["石家庄", "邯郸", "唐山", "张家口", "廊坊"], ["沈阳市", "大连市", "鞍山市", "抚顺市", "本溪市"], ['成都', '绵阳', '眉山', '乐山']] //遍历prov数组,将内容填充进去 for (let i = 0; i < prov.length; i++) { $('#province').append(`<option>${prov[i]} </option>`) } $('#province').change(function () { //将不是请选择的内容移除 $('#cities').children().not(':eq(0)').remove(); // $('#cities').empty() //得到选择的省对应的序号 const index = $(this).children('option:selected').index() // console.log(index); const newcity = city[index - 1] // console.log(newcity); for (let i = 0; i < newcity.length; i++) { $('#cities').append(`<option>${newcity[i]} </option>`) } }) }) </script> </body> </html>
数据添加行,delete删除
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./jquery.min.js"></script> <style> * { padding: 0; margin: 0; } body { margin: 50px; width: 700px; } .top { width: 100%; } h2 { text-align: center; margin-bottom: 20px; } input { width: 160px; margin-right: 10px; } button { display: block; margin: 20px auto; width: 50px; height: 20px; } table { width: 400px; margin: 50px auto; border-collapse: collapse; } th, td { width: 50px; border: 1px solid black; text-align: center; } </style> </head> <body> <div class="top"> <h2>添加新员工</h2> name:<input type="text" class="name"> email:<input type="text" class="email"> salary:<input type="text" class="salary"> <button>submit</button> </div> <hr> <div class="box"> <table> <thead> <th>name</th> <th>email</th> <th>salary</th> <th></th> </thead> <tbody> <tr> <td>tom</td> <td>tom@tom.com</td> <td>5000</td> <td><a href="javascript:;">delete</a></td> </tr> </tbody> </table> </div> <script> $(function () { $('button').on('click', function () { $('table').append(`<tr> <td>${$('.name').val()}</td> <td>${$('.email').val()}</td> <td>${$('.salary').val()}</td><td><a href="javascript:;">delete</a></td>`) $('input').val('') }) $('table').on('click', 'a', function () { $(this).parents('tr').remove() }) //动态创建的元素进行操作时,需要将事件绑定在已经静态存在的父级上 // $('a').on('click',function () { // $(this).parent().parent().remove() // }) }) </script> </body> </html>