1, jQuery 中, $(document).ready()是什么意思?
.ready(handler)
當(dāng)DOM準(zhǔn)備就緒的時(shí)候,指定一個(gè)函數(shù)執(zhí)行
等價(jià)于下面這兩種寫(xiě)法:
$.(document).ready(handler)
$(handler)
實(shí)例:
$(function () {
console.log('ready');
})
2,$node.html()和$node.text()的區(qū)別?
$node.html()在沒(méi)有參數(shù)的時(shí)候,獲取集合中的第一個(gè)匹配元素的HTML內(nèi)容,當(dāng)有參數(shù)的時(shí)候表示,設(shè)置每個(gè)匹配元素的html內(nèi)容;
$node.text()在沒(méi)有參數(shù)的時(shí)候,獲取集合中每個(gè)元素的文本內(nèi)容,包括后代(內(nèi)容從父元素往子元素排列),有參數(shù)的時(shí)候,設(shè)置匹配元素集合中的每個(gè)元素的文本內(nèi)容為指定文本內(nèi)容。
3,$.extend 的作用和用法?
jQuery.extend([deep,]target[,object1][,objectN])
- 當(dāng)我們提供兩個(gè)或多個(gè)對(duì)象給$.extend(),對(duì)象的所有屬性都添加到目標(biāo)對(duì)象(target參數(shù))。
- 如果只有一個(gè)參數(shù)提供給$.extend()。這意味著目標(biāo)參數(shù)被省略;在這種情況下,jquery對(duì)象本身被默認(rèn)為目標(biāo)對(duì)象。這樣,我們可以在jQuery的命名空間下添加新的功能。這對(duì)于插件開(kāi)發(fā)者希望向jquery中添加新函數(shù)時(shí)是很有用的
目標(biāo)對(duì)象(第一個(gè)參數(shù))將被修改,并且將通過(guò)$.extend()返回。然而,如果我們想保留原對(duì)象,我們可以通過(guò)傳遞一個(gè)空對(duì)象作為目標(biāo)對(duì)象:
var object = $.extend({},object1,oject2);
在默認(rèn)情況下,通過(guò)$.extend()合并操作不是遞歸的。
如果第一個(gè)對(duì)象的屬性本身是一個(gè)對(duì)象或者數(shù)組,那么它將完全用第二個(gè)對(duì)象相同的key重寫(xiě)一個(gè)屬性。這些值不會(huì)被合并。如果將true作為該函數(shù)的第一個(gè)參數(shù),那么會(huì)在對(duì)象上進(jìn)行遞歸的合并。
使用范例:
var obj1 = {a:1};
var obj2 = {b:2,c:3};
var obj3 = {b:3,d:5};
$.extend(obj1,obj2); //把obj2擴(kuò)展到obj1上去,作一個(gè)遍歷,如果obj2里面有的屬性obj1里面也有,會(huì)進(jìn)行覆蓋,如果obj1里面沒(méi)有,會(huì)做一次新增
// obj1 == {a:1,b:2,c:3}
$.extend(obj1,obj2,obj3);//修改的還是obj1,此時(shí)obj1 == {a:1,b:3,c:3,d:5} 已經(jīng)存在的屬性進(jìn)行覆蓋,沒(méi)有的進(jìn)行新增
// 如果想得到三個(gè)屬性擴(kuò)展的值,但是又不想修改obj1的話
var obj4 = {};
$.extend(obj4,obj1,obj2,obj3);
//也可以向下面這么寫(xiě),直接寫(xiě)個(gè)空對(duì)象,將擴(kuò)展完成的對(duì)象賦給新定義的對(duì)象就好
var obj5 = $.extend({},obj1,obj2,obj3);
4, jQuery 的鏈?zhǔn)秸{(diào)用是什么?
鏈?zhǔn)秸{(diào)用:使用jQuery方法時(shí),對(duì)象方法返回的是對(duì)象本身,可以調(diào)用對(duì)此對(duì)象的其他jQuery方法,實(shí)現(xiàn)連續(xù)調(diào)用多個(gè)方法
例:$(this).addClass('active').siblings().removeClass('active')
5, jQuery 中 data 函數(shù)的作用
在匹配元素上存儲(chǔ)任意相關(guān)的數(shù)據(jù) 或 返回匹配的元素集合中的第一個(gè)元素的給定名稱(chēng)的數(shù)據(jù)存儲(chǔ)的值。
- data(key,value
描述:在匹配元素上存儲(chǔ)任意相關(guān)數(shù)據(jù)
$("body").data("foo" , 18);
$("body").data("abc", { name: "text", sex: 20 });
$("body").data({cba:[a,b,c]});
$("body").data("foo"); // 18
$("body").data() // {foo: 18, abc: {name: "text", sex: 20}, cba:[a,b,c]}
6,寫(xiě)出以下功能對(duì)應(yīng)的 jQuery 方法:
- 給元素 $node 添加 class active,給元素 $noed 刪除 class active
$node.addClass('active');
$node.removeClass('active');
- 展示元素$node, 隱藏元素$node
$node.hide();
$node.show()
- 獲取元素$node 的 屬性: id、src、title, 修改以上屬性
$node.attr('id','newID');
$node.attr('src','newsrc');
$node.attr('tile','newtitle');
- 給$node 添加自定義屬性data-src
$node.data('data-src','src');
- 在$ct 內(nèi)部最開(kāi)頭添加元素$node
$ct.prepend($node);
- 在$ct 內(nèi)部最末尾添加元素$node
$ct.append($node);
- 刪除$node
$node.remove();
- 把$ct里內(nèi)容清空
$ct.empty()
- 在$ct 里設(shè)置 html <div class="btn"></div>
$ct.html('<div class="btn"></div>')
- 獲取、設(shè)置$node 的寬度、高度(分別不包括內(nèi)邊距、包括內(nèi)邊距、包括邊框、包括外邊距)
不包括內(nèi)邊距
$node.height();
$node.width();
包括內(nèi)邊距
$node.innerHeight();
$node.innerWidth();
包括邊框
$node.outerHeight();
$node.outerWidth();
包括邊框
$node.outerHeight(true);
$node.outerWidth(true);
設(shè)置
不包括內(nèi)邊距
$node.height( '100px' );
$node.width( '100px' );
包括內(nèi)邊距
$node.innerHeight( '100px' );
$node.innerWidth( '100px' );
包括邊框
$node.outerHeight( '100px' );
$node.outerWidth( '100px' );
包括邊框
$node.outerHeight( '100px', true );
$node.outerWidth( '100px', true );
- 獲取窗口滾動(dòng)條垂直滾動(dòng)距離
$(window).scrollTop()
- 獲取$node 到根節(jié)點(diǎn)水平、垂直偏移距離
$node.offset().left
$node.offset().top
- 修改$node 的樣式,字體顏色設(shè)置紅色,字體大小設(shè)置14px
$node.css({color:'red,fontSize:'14px'})
- 遍歷節(jié)點(diǎn),把每個(gè)節(jié)點(diǎn)里面的文本內(nèi)容重復(fù)一遍
$node.each(function(){
$(this).text().+$(this).text();
});
- 從$ct 里查找 class 為 .item的子元素
$ct.find('.item')
- 獲取$ct 里面的所有孩子
$ct.children()
- 對(duì)于$node,向上找到 class 為'.ct'的父親,在從該父親找到'.panel'的孩子
$node.parents('.ct').find('.panel')
- 獲取選擇元素的數(shù)量
$('#id').length
- 獲取當(dāng)前元素在兄弟中的排行
$('ul').index();
7,用jQuery實(shí)現(xiàn)以下操作
- 當(dāng)點(diǎn)擊$btn 時(shí),讓 $btn 的背景色變?yōu)榧t色再變?yōu)樗{(lán)色
- 當(dāng)窗口滾動(dòng)時(shí),獲取垂直滾動(dòng)距離
- 當(dāng)鼠標(biāo)放置到$div 上,把$div 背景色改為紅色,移出鼠標(biāo)背景色變?yōu)榘咨?/li>
- 當(dāng)鼠標(biāo)激活 input 輸入框時(shí)讓輸入框邊框變?yōu)樗{(lán)色,當(dāng)輸入框內(nèi)容改變時(shí)把輸入框里的文字小寫(xiě)變?yōu)榇髮?xiě),當(dāng)輸入框失去焦點(diǎn)時(shí)-
- 去掉邊框藍(lán)色,控制臺(tái)展示輸入框里的文字
- 當(dāng)選擇 select 后,獲取用戶選擇的內(nèi)容
demo
8,用 jQuery ajax 實(shí)現(xiàn)加載更多
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!--<script src="../jquery-3.2.1.min.js"></script>-->
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
#ct li {
border: 1px solid #ccc;
padding: 10px;
margin-top: 10px;
margin-left: 8px;
margin-right: 8px;
cursor: pointer;
}
#load-more {
display: block;
margin: 10px auto;
text-align: center;
cursor: pointer;
}
#load-more img {
width: 40px;
height: 40px;
}
.btn {
display: inline-block;
height: 40px;
line-height: 40px;
width: 80px;
border: 1px solid #e27272;
border-radius: 3px;
text-align: center;
text-decoration: none;
color: #e27272;
}
.btn:hover,li:hover {
background: green;
color: #fff;
}
</style>
<body>
<ul id="ct">
<li>內(nèi)容1</li>
<li>內(nèi)容2</li>
<!--加載更多,從后端得到更多的li放到ul中就可以了-->
</ul>
<a href="javascript:void(0)" id="load-more" class="btn">加載更多</a>
<script>
var currentIndex = 2;
var currentLength = 5;
$('#load-more').on('click',function () {
$.ajax({
url: '/loadMore',
method: 'GET',
data: {
index: currentIndex,
length: currentLength,
}
}).done(function (ret) {
render(ret);
currentIndex += currentLength;
})
})
function render (data) {
var html = '';
var $data = $(data);
$data.each(function (i,e) {
html += '<li>' + e + '</li>';
})
$('#ct').append(html);
}
</script>
</body>
</html>
router.js
app.get('/loadMore',function (req,res) {
var index = req.query.index;
var length = req.query.length;
var data = [];
for (var i = 0; i < length; i ++ ) {
index++;
data[i] = '內(nèi)容' + parseInt(index);
}
res.send(data);
})