常用的js代码

1.输入框输入提示代码

<input name="keyword"  type="text"  value="请输入关键字" onFocus="this.value=''" onBlur="if(!value){value=defaultValue;}">

2.全选功能

<table>
            <tr><td><input type="checkbox" onclick="checkall(this)">全选</td></tr>
            <tr><td><input type="checkbox"></td></tr>
            <tr><td><input type="checkbox"></td></tr>
            <tr><td><input type="checkbox"></td></tr>
</table>
//全选 、全不选
function checkall(obj){
var chk = document.getElementsByTagName("input"); for(var i=0;i<chk.length;i++){ if(chk[i].type =="checkbox"){ chk[i].checked = obj.checked; } } }
//全选
 function selectAll(checkbox) {
                $('input[type=checkbox]').prop('checked',$(checkbox).prop('checked'));
 }

<input type="checkbox" onclick="selectAll(this);" />全选<br/>
<input type="checkbox"  /><br/>
<input type="checkbox"  /><br/>
<input type="checkbox"  /><br/>
<input type="checkbox"  /><br/>
<input type="checkbox"  /><br/>
//反选
function isCheck(obj){
var chk = document.getElementsByTagName("input"); for (var i = 1; i < chk.length; i++) { if(chk[i].type =="checkbox"){ obj.checked =(obj.checked==true)?false:true; chk[i].checked=(chk[i].checked==true)?false:true; } } }
//全选  ---jquery实现
$("#checkAll").click(function() { $('input[name="subBox"]').attr("checked",this.checked); }); <input id="checkAll" type="checkbox" />全选 <input name="subBox" type="checkbox" />项1 <input name="subBox" type="checkbox" />项2 <input name="subBox" type="checkbox" />项3 <input name="subBox" type="checkbox" />项4
原文地址:https://www.cnblogs.com/wanliyuan/p/3817074.html