jQuery針對各類元素操作基礎(chǔ)教程
本文實例講述了jQuery針對元素的操作,包括基礎(chǔ)操作、選擇要操作的元素及處理DOM元素等。對jQuery的學(xué)習(xí)有很好的借鑒價值。分享給大家供大家參考之用。具體分析如下:
1、基礎(chǔ)
jquery對象集:
$():jquery對象集合
獲取jquery對象集中的元素:
使用索引獲取包裝器中的javascript元素:
var temp = $('img[alt]')[0]
使用jquery的get方法獲取jquery對象集中的javascript元素:
var temp = $('img[alt]').get(0)
使用jquery的eq方法獲取jquery對象集中的jquery對象元素:
$('img[alt]').eq(0)
$('img[alt]').first()
$('img[alt]').last()
jquery對象集轉(zhuǎn)換成javascript數(shù)組:
var arr = $('label+button').toArray()
label后面所有同級button元素,轉(zhuǎn)換成javascript數(shù)組
jquery對象集的索引:
var n = $('img').index($('img#id')[0]) 注意:index()參數(shù)是javascript元素
var n = $('img').index('img#id') 等同于上一行 找不到返回-1
var n = $('img').index() 獲得img在同級元素中的索引
向jquery對象集中添加更多的jquery對象集:
使用逗號:
$('img[alt],img[title]')
使用add方法:
$('img[alt]').add('img[title]')
對不同的jquery對象集中采取不同的方法:
$('img[alt]').addClass('thickBorder').add('img[title]').addClass('');
向jquery對象集中添加新創(chuàng)建的元素:
$('p').add('<div></div>');
刪除jquery對象集中的元素:
$('img[title]').not('[title*=pu]') $('img').not(function(){return !$(this).hasClass('someClassname')})
過濾jquery對象集:
$('td').filter(function(){return this.innerHTML.match(^\d+$)})過濾包含數(shù)字的td
獲取jquery對象集的子集
$('*').slice(0,4) 包含前4個元素的新的jquery對象集
$('*').slice(4) 包含前4個元素的新的jquery對象集
$('div').has('img[alt]')
轉(zhuǎn)換jquery對象集中的元素:
var allIds = $('div').map(function(){ return (this.id==undefined) ? null : this.id; }).get();
上述示例可通過get方法轉(zhuǎn)換成javascript數(shù)組。
遍歷jquery對象集中的元素:
$('img').each(function(n){ this.alt = '這是第['+n+']張圖片,圖片的id是' + this.id; }) $([1,2,3]).each(function(){alert(this);})
使用元素間關(guān)系獲取jquery對象集:
$(this).closest('div')比如觸發(fā)的按鈕在哪個div中發(fā)生
$(this).siblings('button[title="Close"]')所有同級元素,不包含本身
$(this).children('.someclassname')所有子節(jié)點元素,不包含重復(fù)子節(jié)點
$(this).closest('')臨近祖先元素
$(this).contents()由元素內(nèi)容組成的jquery對象集,比如可以獲取<iframe>元素內(nèi)容
$(this).next('.someclassname')下一個同級元素
$(this).nextAll()后面所有的同級元素
$(this).nextUntil('.someclassname')后面所有的同級元素直到遇到目標元素
$(this).offsetParent()離jquery對象集最近的父輩元素
$(this).parent()直接父元素
$(this).parents()所有父元素
$(this).parrentsUntil()所有父元素,直到目標父元素
$(this).prev()上一個同級元素
$(this).prevAll()之前的所有同級元素
$(this).prevTntl()之前的所有同級元素,直到目標元素
其它獲取jquery對象集的方式:
$(this).find(p span)
判斷是否是某個jquery對象集:
var hasImg = $('*').is('img');
jquery方法:
$().hide()
$().addClass('')
$().html('')
$('a').size()元素數(shù)量
jquery選擇器:
$('p:even')
$('tr:nth-child(1)')
$('body > div')直接子元素
$('a[href=$='pdf']')根據(jù)屬性選擇
$(div:has(a))過濾
jquery函數(shù):
$.trim()
jquery執(zhí)行時間:
$(document).ready(function(){});
$(function(){});
創(chuàng)建DOM元素:
$('<p></p>').insertAfter(); $('<img>',{ src: '', alt: '', title: '', click: function(){} }).css({ cursor:'pointer', border:'', padding:'', backgroundColor:'white' }).append('');
jquery擴展:
$.fn.disable = function(){ return this.each(function(){ if(this.disabled != null) this.disabled = true; }) }; $('').disable();
jquery測試元素是否存在:
if(item)(){}else{} 寬松測試
if(item != null) 推薦測試,能把null和undefined區(qū)別開
2、選擇要操作的元素
根據(jù)標簽名:$('a')
根據(jù)id:$('#id')
根據(jù)類名:$('.someclassname')
滿足多個條件:$('a#id.someclassname') 或 $('div,span')
某個元素的所有子節(jié)點:$(p a.someclassname)
某個元素的直接子節(jié)點:$(ul.myList > li)
根據(jù)屬性名:
$(a[href^='http://']) 以...開頭
$(href$='.pdf')以...結(jié)尾
$(form[method])包含method屬性的form
$(intput[type='text'])
$(intput[name!=''])
$(href*='some')包含
某元素后的第一個元素:$(E+F)匹配的是F,F(xiàn)是E后面的第一個元素
某元素后的某一個元素:$(E~F)匹配的是F,F(xiàn)是E后面的某一個元素
通過位置:
$(li:first)第一個li
$(li:last)最后一個li
$(li:even)偶數(shù)行l(wèi)i
$(li:odd)奇數(shù)行l(wèi)i
$(li:eq(n))第n個元素,索引從0開始
$(li:gt(n))第n個元素之后的元素,索引從0開始
$(li:lt(n))第n個元素之前的元素,索引從0開始
$(ul:first-child)列表中的第一個li
$(ul:last-child)列表中的最后一個li
$(ul:nth-child(n))列表中的第n個li
$(ul:only-child)沒有兄弟li的ul
$(ul:nth-child(even))列表中的偶數(shù)行l(wèi)i,odd為計數(shù)行l(wèi)i
$(ul:nth-child(5n+1))列表中被5除余1的li
通過過濾器:
$(input:not(:checkbox))
$(':not(img[src*="dog"])')
$('img:not([src*="dog"])')
$(div:has(span))
$('tr:has(img[src$="pu.png"])')
$(tr:animated)處于動畫狀態(tài)的tr
$(input:button)包括type類型為button,reset,submit的Input
$(input:checkbox)等同于$(input[type=checkbox])
$(span:contains(food))包含文字food的span
$(input:disabled)禁用
$(input:enabled)啟用
$(input:file)等同于$(input[type=file])
$(:header)h1到h6
$(input:hidden)
$(input:image)等同于$(input[type=image])
$(:input)包括input, select, textarea, button元素
$(tr:parent)
$(input:password)等同于$(input[type=password])
$(input:radio)等同于$(input[type=radio])
$(input:reset)等同于$(input[type=reset])或$(button[type=reset])
$('.clssname:selected')
$(input:submit)等同于$(input[type=submit])或$(button[type=submit])
$(input:text)等同于$(input[type=text])
$(div:visible)
3、處理DOM元素
操作元素的屬性:
$('*').each(function(n){ this.id = this.tagName + n; })
獲取屬性值:
$('').attr('');
設(shè)置屬性值:
$('*').attr('title', function(index, previousValue){ return previousValue + ' I am element ' + index + ' and my name is ' + this.id; }) //為一個屬性設(shè)置值 $('input').attr({ value: '', title: '' }); //為多個屬性設(shè)置值
刪除屬性:
$('p').removeAttr('value');
讓所有鏈接都在新窗口中打開:
$('a[href^="http://"]').attr('target',"_blank");
避免表單多次提交:
$("form").submit(function(){ $(":submit", this).attr("disabled","disabled"); })
添加類名:
$('#id').addClass('')
刪除類名:
$('#id').removeClass('')
切換類名:
$('#id').toggleClass('')
存在就刪除類名,不存在就添加類名
判斷是否含有類名:
$('p:first').hasClass('') $('p:first').is('')
以數(shù)組形式返回類名列表:
$.fn.getClassNames = function(){ var name = this.attr('someclsssname'); if(name != null){ return name.split(" "); } else { return []; } }
設(shè)置樣式:
$('div.someclassname').css(function(index, currentWidth){ return currentWidth + 20; }); $('div').css({ cursor: 'pointer', border: '1px solid black', padding: '12px 12px 20px 20x', bacgroundColor: 'White' });
有關(guān)尺寸:
$(div).width(500)
$('div').height()
$('div').innerHeight()
$('div').innerWidth()
$('div').outerHeight(true)
$('div').outerWidth(false)
有關(guān)定位:
$('p').offset()相對于文檔的參照位置
$('p').position()偏移父元素的相對位置
$('p').scrollLeft()水平滾動條的偏移值
$('p').scrollLeft(value)
$('p').scrollTop()
$('p').scrollTop(value)
有關(guān)元素內(nèi)容:
$('p').html()
$('p').html('')
$('p').text()
$('p').text('')
追加內(nèi)容
在元素末尾追加一段html:
$('p').append('<b>some text</b>');
在元素末尾dom中現(xiàn)有的元素:
$('p').append($(a.someclassname))
在元素開頭追加:
$("p").prepend()
在元素的前面追加:
$("span").before()
在元素的后面追加:
$("span")after()
把內(nèi)容追加到末尾:
appendTo(targets)
把內(nèi)容追加到開頭:
prependTo(targets)
把內(nèi)容追加到元素前面:
insertBefore(targets)
把內(nèi)容追加到元素后面:
$('<p></p>').insertAfter('p img');
包裹元素:
$('a.someclassname').wrap("<div class='hello'></div>")
$('a.someclassname').wrap($("div:first")[0])
$('a.someclassname').wrapAll()
$('a.someclassname').wrapInner()
$('a.someclassname').unWrap()
刪除元素:
$('.classname').remove()刪除元素,綁定到元素上的事件和數(shù)據(jù)也會被刪除
$('.classname').detach()刪除元素,但保留事件和數(shù)據(jù)
$('.classname').empty()不刪除元素,但清空元素內(nèi)容
復(fù)制元素:
$('img').clone().appendTo('p.someclassname')
$('ul').clone().insertBefore('#id')
替換元素:
$('img[alt]').each(function(){ $(this).replaceWith('<span>' + $(this).attr('alt') + '</span>'); }) $("p").replaceAll("<b></b>")
關(guān)于表單元素的值:
$('[name="radioGroup"]:checked').val()獲取單選按鈕的值,如果沒有選中一個,返回undefined var checkboxValues = $('[name="checkboxGroup"]:checked').map(function(){ return $(this).val(); }).toArray(); //獲取多選框的值
對于<select id="list" multiple="multiple">使用$('#list').val()返回值的數(shù)組
$('input').val(['one','two','three'])如果單選框或復(fù)選框與數(shù)組中的元素匹配,則選中狀態(tài)
相信本文所述對大家的jQuery程序設(shè)計有一定的借鑒價值。
相關(guān)文章
淺析jquery ajax異步調(diào)用方法中不能給全局變量賦值的原因及解決方法
本篇文章主要是對jquery ajax異步調(diào)用方法中不能給全局變量賦值的原因及解決方法進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01JQuery 應(yīng)用 JQuery.groupTable.js
在以往的項目中,數(shù)據(jù)瀏覽用的都table 的形式展現(xiàn)。如下圖1, 這是一個很常見的表格。通常對分類的字段先進行排序。為了讓分類表現(xiàn)的更直觀。2010-12-12使用jQuery全局事件ajaxStart為特定請求實現(xiàn)提示效果的代碼
首先,重寫Ajax方法的代價太高,仍然可以利用jQuery自身的Ajax Events。2010-12-12jQuery實現(xiàn)最簡單的切換圖效果【可兼容IE6、火狐、谷歌、opera等】
這篇文章主要介紹了jQuery實現(xiàn)最簡單的切換圖效果,可兼容IE6、火狐、谷歌、opera等瀏覽器,涉及jQuery結(jié)合時間函數(shù)動態(tài)操作頁面元素的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下2016-09-09jquery?追加元素append、prepend、before、after用法與區(qū)別分析
在jquery中append、prepend、before、after方法是對數(shù)據(jù)元素節(jié)點的操作系列方法了,這些方法大家了解嗎?如果不了解就可以和小編來看看它們用法2016-12-12jquery教程限制文本框只能輸入數(shù)字和小數(shù)點示例分享
這篇文章主要介紹了JQuery限制文本框只能輸入數(shù)字和小數(shù)點的方法,大家參考使用吧2014-01-01