三元运算:
var a = if 条件?真值:假值
全选,反选
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src='jquery-3.4.0.js'></script> </head> <body> <table border="1px" width="200px"> <thead> <tr id="i1"> <th><input type="button" value="全选" οnclick="f1()"/></th> <th><input type="button" value="取消" οnclick="f2()"/></th> <th><input type="button" value="反选" οnclick="f3()"/></th> </tr> </thead> <tbody > <tr > <td><input type="checkbox" /></td> <td>id</td> <td>port</td> </tr> <tr> <td><input type="checkbox" /></td> <td>123</td> <td>456</td> </tr> <tr> <td><input type="checkbox" /></td> <td>123</td> <td>456</td> </tr> <tr> <td><input type="checkbox" /></td> <td>123</td> <td>456</td> </tr> </tbody> </table> <script> console.log($(':button:first')); function f1() { $('tbody :checkbox').prop('checked',true) } function f2 () { $('tbody :checkbox').prop('checked',false) } function f3(){ $(':checkbox').each(function () { if (this.checked){ this.checked = false } else{ this.checked = true } })} </script> </body> </html>
左侧菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .c1{ background-color: blue; width: 200px; } .hide{ display: none; } </style> <script src="jquery-3.4.0.js"></script> </head> <body> <div class="c1" > 菜单1 </div> <div class="hide"> <div>内容一</div> <div>内容一</div> <div>内容一</div> </div> <div class="c1" > 菜单2 </div> <div class="hide"> <div>内容一</div> <div>内容一</div> <div>内容一</div> </div> <div class="c1" > 菜单3 </div> <div class="hide"> <div>内容一</div> <div>内容一</div> <div>内容一</div> </div> <script> console.log($('.c1')); $('.c1').click(function () { $('.c1').next().addClass('hide'); $(this).next().removeClass('hide') }) </script> </body> </html>
模态对话框:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .page{} .edit{} .hide{ display: none; } .shadow{ position: fixed; right: 0px; left: 0px; top: 0px; bottom: 0px; background-color: grey; opacity: 0.5; z-index: 5; } .windows{ position: fixed; width: 500px; height: 300px; left: 50%; top: 50%; margin-top: -150px; margin-left: -250px; background-color: white; z-index: 6; } </style> </head> <body> <div class="shadow hide"> </div> <div class="windows hide"> <p><input type="text"></p> <p><input type="password"></p> <p><input type="button" value="ok"></p> <p><input type="button" value="cancel"></p> </div> <div class="page"> <input type="button" value="add" > <table border="1px" > <thead> <tr> <th>No.</th> <th>ip</th> <th>port</th> <th>edit</th> </tr> </thead> <tbody> <tr > <td>1</td> <td>192.163.0.0</td> <td>8888</td> <td class="edit">edit</td> </tr> <tr> <td>2</td> <td>192.163.0.1</td> <td>8000</td> <td class="edit">edit</td> </tr> <tr> <td>2</td> <td>192.163.0.2</td> <td>8008</td> <td class="edit">edit</td> </tr> </tbody> </table> </div> <script src="jquery-3.4.0.js"></script> <script> $('.page :button').click(function () { $('.shadow, .windows').removeClass('hide') }); $('.edit').click(function () { $('.shadow, .windows').removeClass('hide'); var idd = $(this).prev().prev().text() $('.windows :text').val(idd) var idd = $(this).prev().text() $('.windows :password').val(idd) }) </script> </body> </html>