head > meta charset ="UTF-8" > meta name ="viewport" content ="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" >"> 跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

JQuery 更改属性 JQ对象循环 each 全选反选 三元运算

doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Documenttitle> head> <body> <table border="1"> <thead> <tr> <th>选项th> <th>ipth> <th>portth> tr> thead> <tbody id="i1"> <tr > <td><input type="checkbox">td> <td>1.1.1.1td> <td>80td> tr> <tr> <td><input type="checkbox">td> <td>1.1.1.1td> <td>80td> tr> <tr> <td><input type="checkbox">td> <td>1.1.1.1td> <td>80td> tr> <tr> <td><input type="checkbox">td> <td>1.1.1.1td> <td>80td> tr> tbody> table> <input type="button" value="all" onclick="checkAll()"> <input type="button" value="rev" onclick="reverse()"> <input type="button" value="can" onclick="checkNone()"> body> <script src="jquery-3.2.1.js"> script> <script> function checkAll () { $('#i1 input').prop('checked', true) // $(':checkbox').prop('checked', true) // 实际上, 这两条都是一样的.$带出的JQuery对象只能是列表, 且是标签列表. } function checkNone () { $('#i1 input').prop('checked',false) } // function reverse() { // $('#i1 input').each(function (k) { // console.log(k,this); // }) function reverse() { $('#i1 input').each(function () { //三元运算, 实现反选, 选中的不选, 没选的选中 var v = (this).prop('checked')?false:true; $(this).prop('checked',v) }) } script> html>
在反选里面, 我们不需要用到for循环, 而用封装好的.each(function(k){}) .each里的函数的参数k, 实际上是下标(索引序号)
function reverse() { $('#i1 input').each(function (k) { console.log(k,this); })
输出的结果包含: k  :  0 1 2 3 this: 所有inputs里面的每一个需要循环的input子标签, 而所有的 this实际上都是DOM对象 而不是JQ对象 基于jQuery的AJAX和JSON的实例 中文WORD版 本文档主要讲述的是基于jQuery的AJAX和JSON的实例;过jQuery内置的AJAX功能,直接访问后台获得JSON格式的数据,然后通过jQuer把数据绑定到事先设计好的html模板上,直接在页面上显示。有需要的朋友可以下载看看 下载 如果要把this 转为JQ对象, 需要用$(this)包起来 三元运算: var v = 条件? 真值:假值 学术或足球分析交流微信:chinamaths(进讨论组) Table goes in the document body Don't hesitate to comment or add a like    -    Yours BillBill's技术博客足球分析博客足彩数据视频比尔极客日志_博客园比尔足球数据_网易博客足彩TV_优酷比尔极客日志_CSDN比尔足球数据_新浪博客足彩TV_搜狐视频比尔极客日志_51CTO比尔足球数据_新浪微博足彩TV_喜马拉雅比尔极客日志_开源中国比尔足球数据_官方URL足彩TV_56视频比尔极客日志_GitHub比尔足球数据_头条号微信号:zucai99

相关文章