<a href="#"> href="#" —— #标识,不会重新加载,页面跳到页首</a><br>
<a href=""> href="" —— 空链接,会重新加载url,页面跳到页首</a><br>
<a href="javascript:void(0)"> href="javascript:void(0)" —— 死链接,页面不会跳动</a>
checkBox 选中
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript">
function fn_disabled() {
//$("input[type='checkbox']").attr("disabled", "disabled");
//$("input[type='checkbox']").attr("disabled", true);
//$("input[type='checkbox']").prop("disabled", true);
$("input[type='checkbox']").prop("disabled", "disabled");
}
function fn_enable() {
// $("input[type='checkbox']").removeAttr("disabled");
// $("input[type='checkbox']").attr("disabled", false);
// $("input[type='checkbox']").prop("disabled","");
// $("input[type='checkbox']").prop("disabled", false);
$("input[name='box']").prop("disabled", false);
}
//获取选中的 checkbox的多值
function getCheckedValues() {
var arr = [];
$("input[type='checkbox']:checked").each(function (index, item) {//
arr.push($(this).val());
});
alert(arr);
}
//获取选中的 checkbox的值,只获取匹配的第一个元素的值
function getCheckedValue() {
var checkLength=$("input[type='checkbox']:checked");
alert("选中了"+checkLength.length+"个");
var checkValue=$("input[type='checkbox']:checked").val();
alert("获取匹配的第一个值:"+checkValue);
}
function checkedSecond() {
// $("input[type='checkbox']:eq(1)").prop("checked", "checked");
$("input[type='checkbox']:eq(1)").prop("checked", true);
}
function uncheckedSecond() {
// $("input[type='checkbox']:eq(1)").prop("checked", "");
$("input[type='checkbox']:eq(1)").prop("checked", false);
}
$("#cbCheckbox1").click(function () {
if ($(this).prop("checked")) {//jquery 1.6以前版本 用 $(this).attr("checked")
alert("选中");
} else {
alert("没有选中");
}
});
</script>
<body>
<h3>jQuery操作checkbox</h3>
<hr>
<input type="checkbox" name="box" id="cbCheckbox1" value="1" />
<input type="checkbox" name="box" value="2" />
<input type="checkbox" name="box" disabled="disabled" value="3" />
<input type="checkbox" name="box" value="4" />
<input type="checkbox" name="box" disabled="true" value="5" />
<br />
<input type="button" id="btnDisabled" value="禁用" onclick="fn_disabled();" />
<input type="button" id="Button1" value="启用" onclick="fn_enable();" /><br />
<input type="button" id="Button2" value="获取选中的多值" onclick="getCheckedValues();" /><br />
<input type="button" id="Button2" value="获取选中的单值" onclick="getCheckedValue();" /><br />
<input type="button" id="Button3" value="选中第二个" onclick="checkedSecond();" />
<input type="button" id="Button4" value="取消选中第二个" onclick="uncheckedSecond();" /><br />
<hr/>
</body>
</html>
<script type="text/javascript">
//注意: 操作checkbox的checked,disabled属性时jquery1.6以前版本用attr,1.6以上(包含)建议用prop
//1、根据id获取checkbox
$("#cbCheckbox1");
//2、获取所有的checkbox
$("input[type='checkbox']");//or
$("input[name='cb']");
//3、获取所有选中的checkbox
$("input:checkbox:checked");//or
//$("input:[type='checkbox']:checked");//or
//$("input[type='checkbox']:checked");//or
//$("input:[name='ck']:checked");
//4、获取checkbox值
//用.val()即可,比如:
$("#cbCheckbox1").val();
//5、获取多个选中的checkbox值
var vals = [];
$('input:checkbox:checked').each(function (index, item) {
vals.push($(this).val());
});
//6、判断checkbox是否选中(jquery 1.6以前版本 用 $(this).attr("checked"))
$("#cbCheckbox1").click(function () {
if ($(this).prop("checked")) {
alert("选中");
} else {
alert("没有选中");
}
});
//7、设置checkbox为选中状态
$('input:checkbox').attr("checked", 'checked');//or
$('input:checkbox').attr("checked", true);
//8、设置checkbox为不选中状态
$('input:checkbox').attr("checked", '');//or
$('input:checkbox').attr("checked", false);
//9、设置checkbox为禁用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").attr("disabled", "disabled");//or
$("input[type='checkbox']").attr("disabled", true);//or
$("input[type='checkbox']").prop("disabled", true);//or
$("input[type='checkbox']").prop("disabled", "disabled");
//10、设置checkbox为启用状态(jquery<1.6用attr,jquery>=1.6建议用prop)
$("input[type='checkbox']").removeAttr("disabled");//or
$("input[type='checkbox']").attr("disabled", false);//or
$("input[type='checkbox']").prop("disabled", "");//or
$("input[type='checkbox']").prop("disabled", false);
</script>
checkBox-自练
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript">
// 全选
// $(function(){
// $("#select-all").click(function(){
// $("input[type='checkbox']").prop("checked","checked");
// })
// })
// 全选 [注意:prop 是 jQuery-1.6之后才能使用; 之前可用 attr]
// 使用prop,不会把checked属性加到标签内,【可以多次勾选】
// 但是attr,则会把checked属性写到标签内,【只能勾选一次】
function selectAll(){
$("input[type='checkbox'][name='mybox']").prop("checked","checked"); //或者:
// $("input[type='checkbox']").prop("checked",true); // 或者:
// $("input[type='checkbox']").prop("checked","666");
$("#tip").css("color","red");
}
// 取消全选
function cancelAll(){
$("input[type='checkbox'][name='mybox']").prop("checked","");
$("#tip").css("color","");
}
// 获取已选中的所有复选框的值
function selectAllValues(){
var checkVals=[];
$("input[type='checkbox'][name='mybox']:checked").each(function(){
//checkVals[i]=$(this).val(); // 前提是:function(i)
//或者
checkVals.push($(this).val());
});
alert("选中元素个数:"+checkVals.length+"\r所有元素值为:"+checkVals);
}
// 获取已选中的所有复选框的值2(funtion(index,item)
function selectAllValues2(){
var checkVals=$("input[type='checkbox'][name='mybox']:checked");
var Vals=[];
checkVals.each(function(index,item){
// index为索引,item 当前为单个dom元素,不是jquery对象,不能用val()
// alert(index+"---"+item.value);
Vals.push(item.value);
});
alert("选中元素个数:"+Vals.length+"\r所有元素值为:"+Vals);
}
/* -------------------------------------------------------------------------*/
// 全选
// 使用attr,则会把checked属性写到标签内,【只能勾选一次】
function selectAll_attr(){
$("input[type='checkbox'][name='box']").attr("checked","checked");
$("#tip2").css({"color":"green","font-weight":"bold"});
}
// 取消全选
function cancelAll_removeAttr(){
$("input[type='checkbox'][name='box']").removeAttr("checked");
$("#tip2").css("color","").css("fontWeight","");
}
/* -------------------------------------------------------------------------*/
// 包在$(function())内
$(function(){
$("input[type='checkbox'][name='cx']").click(function(){
// 为每个checkbox 设置click事件
$(this).attr("checked",true);//设置当前选中checkbox的状态为checked
$(this).siblings().attr("checked",false); //设置当前选中的checkbox同级(兄弟级)其他checkbox状态为未选中
})
})
// 获取已选中的复选框的值
function selectValue(){
var checkVal=$("input[type='checkbox'][name='cx']:checked");
alert("选中元素个数:"+checkVal.length+"\r所有元素值为:"+checkVal.val());
}
</script>
<body>
<div>
<fieldset>
<legend>复选框 多选</legend>
<br>
<input type="checkbox" name="mybox" value="111" checked>选项1
<input type="checkbox" name="mybox" value="222">选项2
<input type="checkbox" name="mybox" value="333">选项3
<input type="checkbox" name="mybox" value="444">选项4
<input type="checkbox" name="mybox" value="555">选项5
<br><br><br>
<input type="button" id="select-all" value=" 全选(prop) " onClick="selectAll()">
<input type="button" id="cancel-all" value=" 取消全选(prop) " onClick="cancelAll()">
<span id="tip">prop,可以多次勾选;attr,只能勾选一次</span>
<br><br>
<input type="button" id="select-all-values" value=" 获取全选元素值" onClick="selectAllValues()">
<input type="button" id="select-all-values" value=" 获取全选元素值2(funtion(index,item)" onClick="selectAllValues2()">
<br><hr><br>
<input type="checkbox" name="box" value="1" checked>选项1
<input type="checkbox" name="box" value="2">选项2
<input type="checkbox" name="box" value="3">选项3
<input type="checkbox" name="box" value="4">选项4
<input type="checkbox" name="box" value="5">选项5
<br><br><br>
<input type="button" id="select-all" value=" 全选(attr) " onClick="selectAll_attr()">
<input type="button" id="cancel-all" value=" 取消全选(removeAttr) " onClick="cancelAll_removeAttr()">
<span id="tip2">prop,可以多次勾选;attr,只能勾选一次</span>
</fieldset>
</div>
<hr><hr>
<div>
<fieldset>
<legend>复选框 单选</legend>
<br>
<input type="checkbox" name="cx" value="AAA">选项A
<input type="checkbox" name="cx" value="BBB">选项B
<input type="checkbox" name="cx" value="CCC">选项C
<input type="checkbox" name="cx" value="DDD">选项D
<input type="checkbox" name="cx" value="EEE">选项E
<br><br><br>
<input type="button" id="select-all-value" value=" 获取选中元素值" onClick="selectValue()">
</fieldset>
</div>
<hr/>
</body>
</html>
DOM_sort排序
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript">
//我们选择了三个元素
console.log($('.sel') instanceof jQuery);
//我们选择了第一个div,它是一个JQ对象,不可以使用dom属性和方法
$('.sel').eq(0);
//我们也是选择了第一个div,但是这是一个dom对象,可以使用dom属性方法,但不可以使用JQ的属性方法
$('.sel')[0];
//选择第一个div并转换为dom对象,同上
$('.sel').eq(0)[0];
//以下错误,dom对象无法使用JQ方法,因为.eq()是JQ的方法
//$('.sel')[0].eq(0);
function onTest(){
var arr = $('li');
arr.sort(function(a,b){
debugger;
return a.innerHTML>b.innerHTML?1:-1;
});//对li进行排序,这里按照从小到大排序
$('ul').empty().append(arr);//清空原来内容添加排序后内容。
}
function myList(){
var sels=$("ul").find("li");
sels.sort(function(a,b){
debugger;
//val AAA=$(a).prop("id");
// val BBB=$(a).prop("id");
return (A>B);
}).appendTo("body");
}
</script>
<body>
<div><a href="#">href="#"</a></div>
<div><a href="">href=""</a></div>
<ul>
<li class="sel" id="3"> 刘备 </li>
<li class="sel" id="2"> 关羽 </li>
<li class="sel" id="1"> 张飞 </li>
</ul>
<hr>
<input type="button" value="点击" onClick="onTest()" >
<hr/>
</body>
</html>
DOM_eq 用法
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript">
//我们选择了三个元素
console.log($('.sel') instanceof jQuery);
//我们选择了第一个div,它是一个JQ对象,不可以使用dom属性和方法
$('.sel').eq(0);
//我们也是选择了第一个div,但是这是一个dom对象,可以使用dom属性方法,但不可以使用JQ的属性方法
$('.sel')[0];
//选择第一个div并转换为dom对象,同上
$('.sel').eq(0)[0];
//以下错误,dom对象无法使用JQ方法,因为.eq()是JQ的方法
//$('.sel')[0].eq(0);
</script>
<body>
<div class="sel"> 刘备 </a>
<div class="sel"> 关羽 </a>
<div class="sel"> 张飞 </a>
<hr/>
</body>
</html>
radio 用法
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript">
// 全选
// $(function(){
// $("#select-all").click(function(){
// $("input[type='checkbox']").prop("checked","checked");
// })
// })
// 获取当前选中值
function selectOne(){
var val=$("input[type='radio'][name='mybox']:checked").val();
alert("当前选中值为:"+val);
}
// 获取所有值
function printAll(){
var vals=$("input[type='radio'][name='mybox']");
// alert("当前选中值为:"+vals.val()); // 直接是打印第一个元素的jquery对象的值
var v=[];
$.each(vals,function(index,item){
v.push(item.value);
})
alert(v);
}
// 获取所有值
function printAll2(){
var vals=$("input[type='radio'][name='mybox']");
// alert("当前选中值为:"+vals.val()); // 直接是打印第一个元素的jquery对象的值
var v=[];
vals.each(function(index,item){
v.push($(this).val());
})
alert(v);
}
</script>
<body>
<div>
<fieldset>
<legend>单选按钮</legend>
<br>
<input type="radio" name="mybox" value="111" checked>选项1
<input type="radio" name="mybox" value="222">选项2
<input type="radio" name="mybox" value="333">选项3
<input type="radio" name="mybox" value="444">选项4
<input type="radio" name="mybox" value="555">选项5
<br><br><br>
<input type="button" value=" 获取选中值 " onClick="selectOne()">
<input type="button" value=" 获取所有值 " onClick="printAll()">
<input type="button" value=" 获取所有值2 " onClick="printAll2()">
</fieldset>
</div>
<hr/>
</body>
</html>
select 用法
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript">
// 全选
// $(function(){
// $("#select-all").click(function(){
// $("input[type='checkbox']").prop("checked","checked");
// })
// })
// 获取当前选中值
function selectOne(){
var val=$("select[id='selectList']").val();
// 上面这行,等同于
var txt=$("select[id='selectList']").find("option:selected").val();
var txt=$("select[id='selectList']").find("option:selected").text();
// 获取索引
var index=$("select[id='selectList']").find("option:selected").index();
alert("当前选中值为:"+val+"\n当前选中文本为:"+txt+"\n当前索引值为:"+index);
}
// 获取所有值
function printAll(){
var list=$("select[id='selectList']").find("option");
// alert("当前选中值为:"+vals.val()); // 直接是打印第一个元素的jquery对象的值
var opts=[];
var texts=[];
$.each(list,function(index,item){
// if(index==0){
// alert(item.innerText+":"+item.innerText.length);
// }
str=item.innerText.replace(/^\s+|\s+$/g,"");//原生js去掉两头空格
// 去掉“请选择”
if(str!='请选择'){
opts.push(item.value);
texts.push(item.innerText);
}
})
alert(opts+"\n"+texts);
}
// 获取所有值
function printAll2(){
var list=$("select[id='selectList']").find("option");
// alert("当前选中值为:"+vals.val()); // 直接是打印第一个元素的jquery对象的值
var v=[];
var t=[];
list.each(function(index){
if(index==0){
alert($(this).text().trim()+":"+$(this).text().trim().length);
}
// 去掉“请选择”
if($(this).text().trim()!="请选择"){
v.push($(this).val());
t.push($(this).text());
}
})
alert(v+"\n"+t);
}
// 追加选项(option)
function appendOpt(){
$("select[id='selectList']").append("<option value='AAA'> 曹操 </option>");
}
// ================================================================================
// 当前标签内部的html代码,全部打印(不包含自身)
function use_html(){
var html=$("select[id='selectList']").html();
alert("当前列表为:"+html);
}
// 当前标签的html代码,全部打印(包含自身)
function use_outerHTML(){
var outerHtml=$("select[id='selectList']").prop("outerHTML");
alert("当前列表为:"+outerHtml);
}
</script>
<body>
<div>
<fieldset>
<legend>下拉列表</legend>
<br>
英雄:<select id="selectList">
<option value=""> 请选择 </option>
<option value="111"> 刘备 </option>
<option value="222"> 关羽 </option>
<option value="333"> 张飞 </option>
<option value="444"> 马超 </option>
<option value="555"> 黄忠 </option>
</select>
<br><br><br>
<input type="button" value=" 获取选中值 " onClick="selectOne()">
<input type="button" value=" 获取所有值 " onClick="printAll()">
<input type="button" value=" 获取所有值2 " onClick="printAll2()">
<br><br>
<input type="button" value=" 追加选项(option) " onClick="appendOpt()">
<br><br>
<input type="button" value=" 显示html代码(不含自身)(html()) " onClick="use_html()">
<input type="button" value=" 显示html代码(含有自身)(outerHTML) " onClick="use_outerHTML()">
</fieldset>
</div>
<hr/>
</body>
</html>
table 行列选中
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript"></script>
<script type="text/javascript">
function fun(){
// 选中 第1行,第3列
var SingleTD=$("#tab tr:eq(0) td:eq(2)");
// // 添加 css 样式 (单个样式)
SingleTD.css("color","red");
// 添加 css 样式(多个样式,json格式)
// 带连字符-属性 ,改成驼峰写法
SingleTD.css({"fontSize":"24px","fontWeight":"bold"});
alert("选中列数:"+SingleTD.length);
alert(SingleTD.html());
}
function fun2(){
// 选中 第2行+第3行
var Trs=$("#tab tr:eq(1),#tab tr:eq(2)");
// 或者
var Trs=$("#tab").find("tr:eq(1),tr:eq(2)");
alert("选中行数:"+Trs.length);
Trs.css("color","blue");
// 进而选中 第1列,第4列,第5列
var ComplexTD=Trs.find("td:eq(1),td:eq(3),td:eq(4)")
alert("选中列数:"+ComplexTD.length);
ComplexTD.each(function(i){
// 添加 css 样式(多个样式,json格式)
// // 带连字符-属性 ,改成驼峰写法
$(this).css("color","green");
$(this).css({"fontSize":"24px","fontWeight":"bold"})
});
}
function fo(){
/*document.getElementById("myfocus").focus();*/
$("#myfocus").focus();
}
</script>
<body>
<table id="tab">
<tr> <td>111</td> <td>222</td> <td>333</td> <td>444</td> <td>555</td> </tr>
<tr> <td>666</td> <td>777</td> <td>888</td> <td>999</td> <td>000</td> </tr>
<tr> <td>123</td> <td>456</td> <td>789</td> <td>654</td> <td>321</td> </tr>
</table>
文本:<input id="myfocus" type="text" />
<input type="button" value="单项" onclick="fun()"/>
<input type="button" value="多项" onclick="fun2()"/>
<br/>
<hr/>
<input type="button" value="触发光标" onclick="fo()"/>
</body>
</html>
测试专用
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript">
// 全选
// $(function(){
// $("#select-all").click(function(){
// $("input[type='checkbox']").prop("checked","checked");
// })
// })
// 获取当前选中值
function selectOne(){
var val=$("select[id='selectList']").val();
// 上面这行,等同于
var txt=$("select[id='selectList']").find("option:selected").val();
var txt=$("select[id='selectList']").find("option:selected").text();
// 获取索引
var index=$("select[id='selectList']").find("option:selected").index();
alert("当前选中值为:"+val+"\n当前选中文本为:"+txt+"\n当前索引值为:"+index);
}
// 获取所有值
function printAll(){
var list=$("select[id='selectList']").find("option");
// alert("当前选中值为:"+vals.val()); // 直接是打印第一个元素的jquery对象的值
var opts=[];
var texts=[];
$.each(list,function(index,item){
// if(index==0){
// alert(item.innerText+":"+item.innerText.length);
// }
str=item.innerText.replace(/^\s+|\s+$/g,"");//原生js去掉两头空格
// 去掉“请选择”
if(str!='请选择'){
opts.push(item.value);
texts.push(item.innerText);
}
})
alert(opts+"\n"+texts);
}
// 获取所有值
function printAll2(){
var list=$("select[id='selectList']").find("option");
// alert("当前选中值为:"+vals.val()); // 直接是打印第一个元素的jquery对象的值
var v=[];
var t=[];
list.each(function(index){
if(index==0){
alert($(this).text().trim()+":"+$(this).text().trim().length);
}
// 去掉“请选择”
if($(this).text().trim()!="请选择"){
v.push($(this).val());
t.push($(this).text());
}
})
alert(v+"\n"+t);
}
// 追加选项(option)
function appendOpt(){
$("select[id='selectList']").append("<option value='AAA'> 曹操 </option>");
}
// ================================================================================
// 当前标签内部的html代码,全部打印(不包含自身)
function use_html(){
var html=$("select[id='selectList']").html();
alert("当前列表为:"+html);
}
// 当前标签的html代码,全部打印(包含自身)
function use_outerHTML(){
var outerHtml=$("select[id='selectList']").prop("outerHTML");
alert("当前列表为:"+outerHtml);
}
</script>
<body>
<div>
<fieldset>
<legend>下拉列表</legend>
<br>
英雄:<select id="selectList">
<option value=""> 请选择 </option>
<option value="111"> 刘备 </option>
<option value="222"> 关羽 </option>
<option value="333"> 张飞 </option>
<option value="444"> 马超 </option>
<option value="555"> 黄忠 </option>
</select>
<br><br><br>
<input type="button" value=" 获取选中值 " onClick="selectOne()">
<input type="button" value=" 获取所有值 " onClick="printAll()">
<input type="button" value=" 获取所有值2 " onClick="printAll2()">
<br><br>
<input type="button" value=" 追加选项(option) " onClick="appendOpt()">
<br><br>
<input type="button" value=" 显示html代码(不含自身)(html()) " onClick="use_html()">
<input type="button" value=" 显示html代码(含有自身)(outerHTML) " onClick="use_outerHTML()">
</fieldset>
</div>
<hr/>
</body>
</html>