學(xué)習資料
1.獲取棘捣、設(shè)置元素的內(nèi)容
1.1獲取或設(shè)置目標元素的文本內(nèi)容
語法
$(selector).text(); 獲取元素文本內(nèi)容
$(selector).text(content);設(shè)置元素的文本內(nèi)容
$(selector).text(fnction(index,oldContent) {})
參數(shù)說明
index:當前選擇器的索引位置
oldContent: 選擇器的內(nèi)容
text()用于返回一個值時,它會返回所有匹配元素的組合的文本內(nèi)容(會刪除 HTML 標記)
1.2獲取或設(shè)置目標元素的內(nèi)容(含HTML標記)
html()
方法返回或設(shè)置被選元素的內(nèi)容 (inner HTML)
語法
$(selector).html()
$(selector).html(content)
$(selector).html(function(index,oldcontent))
參數(shù)說明
index:當前選擇器的索引位置
oldContent: 選擇器的內(nèi)容
1.3獲取或設(shè)置表單元素的值
元素的值是通過 value 屬性設(shè)置的
語法
$(selector).val()
$(selector).val(content)
$(selector).val(function(index,oldcontent))
參數(shù)說明
index:當前選擇器的索引位置
oldContent: 選擇器的內(nèi)容
例子
<div class="container">
<div class="content">
<div class="txt">
</div>
</div>
</div>
<div class="val">
<input type="text" value="獲取文本內(nèi)容">
</div>
JS
console.log($('.container').text());
console.log($('.content').text());
console.log($('.txt').text());
console.log($('.container').html());
console.log($('.content').html());
console.log($('.txt').html());
console.log($('[type=text]').val());
效果展示
2.新增內(nèi)容
2.1將新增的內(nèi)容插入到指定元素的結(jié)尾append
語法
2.2將新增的內(nèi)容插入指定元素的開頭prepend
語法
$(selector).prepend();
$(selector).prepend(function(index,html) {});
與prependTo
的區(qū)別:
內(nèi)容和選擇器的位置,以及 prependTo() 無法使用函數(shù)來插入內(nèi)容
$(content).prepnedTo(selector)
2.3在被選元素之前插入內(nèi)容before
語法
$(selector).before()
$(selector).before(function(index) {})
2.4在被選元素之后插入內(nèi)容after
語法
$(selector).after()
$(selector).after(function(index) {})
例子
<div class="btn-group">
<button class="btn btn1">append</button>
<button class="btn btn2">prepend</button>
<button class="btn btn3">before</button>
<button class="btn btn4">after</button>
</div>
<div class="content-area">
<div class="area area1"><p>area1</p></div>
<div class="area area2"><p>area2</p></div>
<div class="area area3"><p>area3</p></div>
<div class="area area4"><p>area4</p></div>
</div>
CSS
div{padding:0;margin:0;}
.btn-group{margin:30px 15px;}
.btn{display:inline-block;width:100px;height:34px;border:none;outline:none;line-height:34px;text-align:center;background-color:aquamarine;color:#666;}
.area{float:left;width:100px;height:100px;background-color:cadetblue;margin:10px;}
.areap{width:100px;height:20px;line-height:20px;color:#fff;text-align:center;}
.new-area{float:left;width:100px;height:65px;background-color:aquamarine;}
JS
$('.btn1').click(function() {
var txt1 ='<div class="new-area">new area1</div>';
$('.area1').append(txt1);
});
$('.btn2').click(function() {
var txt2 ='<div class="new-area">new area2</div>';
$('.area2').prepend(txt2);
});
$('.btn3').click(function() {
var txt3 ='<div class="new-area">new area3</div>';
$('.area3').before(txt3);
});
$('.btn4').click(function() {
var txt4 ='<div class="new-area"></div>';
$('.area4').after(txt4);
});
效果展示
3.設(shè)置或獲取元素屬性值
語法
$(selector).attr(attr)
$(selector).attr(attr,value)
$(selector).attr(attr,function(index,oldValue))
參數(shù)說明
attr:需要獲取的屬性
value:需要設(shè)置的屬性值
index: 當前選擇器的索引
oldValue:當前選擇器對應(yīng)屬性的值
4.刪除元素
4.1刪除所選元素及其子元素(刪除本身及子元素)remove()
方法移除被選元素,包括所有文本和子節(jié)點語法
$(selector).remove()
該方法不會把匹配的元素從 jQuery 對象中刪除,因而可以在將來再使用這些匹配的元素。但除了這個元素本身得以保留之外,remove()
不會保留元素的 jQuery 數(shù)據(jù)耀盗。其他的比如綁定的事件、附加的數(shù)據(jù)等都會被移除
4.2刪除被選元素的子元素(只刪除子元素)
從被選元素移除所有內(nèi)容卦尊,包括所有文本和子節(jié)點;
語法
$(selector).empty()
例子
$('.btn1').click(function() {
$('.removeli').remove('.remove li:nth-child(2n)');
});
$('.btn2').click(function() {
$('.emptyli').empty();
});
效果展示
5.CSS類操作
5.1設(shè)置或返回樣式
語法
1 .$(selector).css(attr-name);
獲取一個或多個樣式屬性叛拷,多個屬性值之間使用逗號隔開;
2. $(selector).css(attr-name,value)
$(selector).css(attr-name,function(index,value) {})
設(shè)置單個的CSS樣式岂却;
3. $(selector).css({attr-name1: value1,attr-name2: value2.....})
設(shè)置多個CSS樣式忿薇;
5.2添加一個或多個類
語法
1. $(selector).addClass(classname)
2. $(selector).addClass(classname classname);
添加多個類時,類之間使用空格隔開
3.$(selector).addClass(function(index,oldClass) {})
該方法不會移除已存在的 class
屬性躏哩,僅僅添加一個或多個 class
屬性
5.3刪除類
語法
1.$(selector).removeClass();
移除所有class
2.$(selector).removeClass(classname)
移除指定的class
3.$(selector).removeClass(function(index,oldclass))
5.4添加署浩、刪除類的切換
語法
1. $(selector).toggleClass(class,switch);
該方法檢查每個元素中指定的類。如果不存在則添加類扫尺,如果已設(shè)置則刪除之參數(shù)說明
switch: 是否刪除或只添加筋栋,布爾值
例子
JS
var isClick =true;
$('.btn1').click(function() {
if(isClick) {
$('.s1').css('background-color','pink');
isClick =false;
}else{
$('.s1').css('background-color','darkseagreen');
isClick =true;
}
});
$('.btn2').click(function() {
$('.s2').addClass('new');
});
$('.btn3').click(function() {
if(isClick) {
$('.s3').addClass('h');
isClick =false;
}else{
效果展示
6.尺寸
6.1返回盒模型內(nèi)容區(qū)域的高度
語法
$(selector).height()
6.2返回盒模型內(nèi)容區(qū)域的寬度
語法
$(selector).width()
6.3返回盒模型上下內(nèi)邊距+內(nèi)容區(qū)域的高度
語法
$(selector).innerHeight();
6.4返回盒模型左右內(nèi)邊距+內(nèi)容區(qū)域的寬度
語法
$(selector).innerWidth();
6.5返回盒模型上下邊框+上下內(nèi)邊距+內(nèi)容區(qū)域的高度
語法
$(selector).outerHeight();
6.6返回盒模型左右邊框+左右內(nèi)邊距+內(nèi)容區(qū)域的寬度
語法
$(selector).outerWidth();
6.5返回盒模型上下外邊距+上下邊框+上下內(nèi)邊距+內(nèi)容區(qū)域的高度
語法
$(selector).outerHeight(true)
6.6返回盒模型左右外邊距+左右邊框+左右內(nèi)邊距+內(nèi)容區(qū)域的寬度
語法
$(selector).outerWidth(true)
6.8文檔(HMTL文檔)、窗口(瀏覽器視口)的高度
語法
1.瀏覽器當前窗口文檔的高度
$(document).height()
2.瀏覽器當前窗口可視區(qū)域高度
$(window).height()
6.9文檔(HMTL文檔)正驻、窗口(瀏覽器視口)的寬度
語法
1.瀏覽器當前窗口文檔的寬度
$(document).width()
2.瀏覽器當前窗口可視區(qū)域?qū)挾?/strong>
$(window).width()