JQueryObjectArray.each(function(index,Element))css
$(".myTable").each(function(i,ele){ //使用模板函数 //这里的ele是一个DOM对象,要想使用jQuery对象,能够这样写$(this) //function里面的i和ele两个参数,根据实际状况填 console.log(`${i}: ele.innerText`); });
toFixed(2) 保留2位小数html
数组调用map,会自动拼接成一个字符串es6
$.getJSON('json_data.html', {name1: '参数值', name2: 'value2'}, function(res) { // 服务器响应,返回的json数据 // es6 数组的map() const trArr = res.map(item => { return ` <tr> <td>${item.empno}</td> <td>${item.ename}</td> <td>${item.sal}</td> </tr> ` }); // console.log(...trArr); // join()将数组的元素链接成一个字符串 console.log(trArr.join('')); $('#myDiv').html(` <table class="table"> <tr> <th>编号</th> <th>姓名</th> <th>工资</th> </tr> ${trArr.join('')} </table> `); }); });
得到属性有两种方法json
设置属性也是两种方法,方法名与得到属性的两种方法相同,只不过多了个参数数组
设置全选例子:服务器
<form action=""> <input type="checkbox" id="checkall" >全选 <br> <br> 爱好:<br> <input type="checkbox" name="hobby">读书<br><br> <input type="checkbox" name="hobby">电影<br><br> <input type="checkbox" name="hobby">游戏<br><br> <input type="checkbox" name="hobby">游泳<br><br> <input type="checkbox" name="hobby">写代码<br><br> </form> <script> $(function(){ $('#checkall').click(function(){ console.log(this); if(this.checked){ $(":input[name='hobby']").attr("checked",true); }else{ $(":input[name='hobby']").attr("checked",false); } }) }); </script>
$(':button').removeAttr("name");
addClass没法实现替换,通常经过删除以后再添加来实现替换class的效果ide
$("p").removeClass("myClass noClass").addClass("yourClass");
$('#mydiv').hide(); $('#mydiv').show();
//鼠标移入移出 $("#mybutton").hover(function(){ //这里是鼠标移入后执行的逻辑操做 },function(){ //这里是鼠标移出后执行的逻辑操做 }); //鼠标点击 $("#mybutton").click(function(){ //这里是鼠标点击后执行的逻辑操做 });