jQuery包装集
判断元素是否存在于页面
Js实现
判断getElementById返回的是否等于null或undefined
window.onload = function () { document.getElementById('btn1').onclick = function () { alert('hello'); }; };
|
jQuery实现
判断选择器选择到的元素集合的length
$(function () { alert($('#btn1').length); $('#btn1').click(function () { alert('hello'); }); if($('#btn1').length > 0) { alert('页面上存在该元素!'); } else { alert('页面上不存在该元素!'); } });
|
jQuery对象是一个包装集
本身jQuery对象就是一个dom对象的集合,所以可以直接通过索引访问其中的任何一个对象,并且返回的就是dom对象。
$('#btn')[0]; $('#btn').get(1);
|
通过jQuery操作类样式
增加一个样式
$('body').addClass('night');
|
移除一个样式
$('body').removeClass('night');
|
判断元素是否应用样式
if ($('body').hasClass('night')) { $('body').removeClass('night'); } else { $('body').addClass('night'); }
|
切换类样式的应用与移除
$('body').toggleClass('night');
|
基本过滤选择器
以冒号开头
$('#btn').click(function () { $('p:first').css('backgroundColor', 'red'); $('p:last').css('backgroundColor', 'red'); $('p:eq(1)').css('backgroundColor', 'red'); $('p:even').css('backgroundColor', 'blue'); $('p:odd').css('backgroundColor', 'yellow'); $('p:gt(2)').css('backgroundColor', 'green'); $('p:lt(3)').css('backgroundColor', 'blue'); $('p:not(.cls)').css('backgroundColor', 'pink'); $(':header').css('backgroundColor', 'red'); });
|
总结:
过滤器 |
解释 |
栗子 |
:first |
选取第一个元素 |
$('p:first') |
:last |
选取最后一个元素 |
$('p:last') |
:eq(索引序号) |
选取索引等于索引序号的元素,eq即equal |
$('p:eq(1)') |
:gt(索引序号) |
选取索引大于索引序号的元素,gt即greater than |
$('p:gt(2)') |
:lt(索引序号) |
选取索引小于索引序号的元素,lt即less than |
$('p:lt(3)') |
:even |
选取索引是偶数的元素 |
$('p:even') |
:odd |
选取索引是奇数的元素 |
$('p:odd') |
:not(选择器) |
选取不满足“选择器”条件的元素 |
$('p:not(.cls)') |
:header |
选取所有的h1到h6的标题元素 |
$(':header') |
$(selector, context)用法
表示在context的范围内,搜索selector
$('#t1 tr').click(function () { $('td:even', this).css('backgroundColor', 'blue'); $('td:odd', this).css('backgroundColor', 'red'); });
|
属性过滤选择器
$('input[name=txt1]');
$('input[name]');
$('body *[name]'); $('body [name]');
$('body *[name^=a]');
$('body *[name=]');
$('body *[name$=a]');
$('body *[name*=a]');
$('body *[name!=abc]');
$('body *[name][id][value]');
|
表单对象属性选择器
$(':disabled');
$(':enabled');
$('#form1 :disabled');
$('#form1 :enabled');
$('input:disabled');
$('input:enabled');
$(':checked');
$('div :not(:checked)');
|
checkbox练习
$(function () { $('input[type=checkbox]').click(function () { var chks = $('input[type=checkbox]:checked'); var n = chks.length; var names = []; $.each(chks, function (k, chkObj) { names[names.length] = $(chkObj).val(); }); $('#pmsg').text('共选中了' + n + '项,分别是' + names.toString()); }); });
|
简化类型写法
代替了$('input[type=xxx]')的写法。
$('input[type=checkbox]'); $(':checkbox');
$(':radio');
$(':text');
$(':password');
$(':hidden');
$(':checkbox:checked');
|
注意:
动态创建DOM节点
动态创建超链接
方法一
append:在被选元素的结尾(被选元素里面)插入指定内容。
prepend:在被选元素的开头插入内容。
after:在被选元素后(被选元素的兄弟节点)插入指定的内容
before:在被选元素前插入内容。
var aLinkObj = $('<a href="http://www.baidu.com">百度</a>');
$('#dv1').append(aLinkObj);
|
方法二
appendTo:在被选元素的结尾插入 HTML 元素。
prependTo:在被选元素的开头插入 HTML 元素。
insertAfter:在被选元素后插入 HTML 元素。
insertBefore:在被选元素前插入 HTML 元素。
$('<a href="http://www.baidu.com">百度</a>').appendTo('#dv1');
|
动态创建表格
var websites = { '百度': 'http://www.baidu.com', '新浪': 'http://www.sina.com.cn', '搜狐': 'http://www.sohu.com', '网易': 'http://www.163.com' };
$(function () { $('#btn').click(function () { var tblObj = $('<table border="1"></table>').appendTo('body'); for (var key in websites) { $('<tr><td>' + key + '</td><td><a href="' + websites[key] + '">' + key + '</a></td></tr>').appendTo(tblObj); } }); });
|
删除节点
清空元素
empty
清空某元素下的所有子节点。
$('#btnEmpty').click(function () { var dvObj = $('#dv1'); dvObj.empty(); });
|
删除元素
remove(selector)
删除当前元素,返回值为被删除的元素。
$('#btnRemove').click(function () { $('#dv1').remove(); });
|
练习
五秒钟协议按钮
var sec = 4; $(function () { var intervalId = setInterval(function () { if (sec > 0) { $('#btn').val('请仔细阅读协议(' + sec + ')'); sec--; } else { $('#btn').val('同意').prop('disabled', false); clearInterval(intervalId); } }, 1000); });
|
替换和包裹
替换节点
replaceWith
$(function () { $('#btn').click(function () { $('hr').replaceWith('<a href="#">百度</a>'); }); });
|
replaceAll
$(function () { $('#btn').click(function () { $('<font color="red">==========</font>').replaceAll('hr'); }); });
|
包裹节点
wrap
$('p').wrap('<font color="blue"></font>');
|
wrapInner
$('p').wrapInner('<font color="red"></font>');
|
wrapAll
$('p').wrapAll('<font color="red"></font>');
|