jQuery 中, $(document).ready()是什么意思?
當DOM加載完成時,執(zhí)行其中的代碼。
與原生JavaScript中的load事件需要在頁面所有內容加載完成后才執(zhí)行不同,ready只要在DOM結構加載完成后,就可以執(zhí)行。
$(document).ready(function(){
});
可簡寫為:
$(function(){
})
$node.html()和$node.text()的區(qū)別?
$node.html()取得或設置配元素的html內容。
$node.text()取得或設置所有匹配元素的文本內容。
$.extend 的作用和用法?
當我們提供兩個或多個對象給$.extend(),對象的所有屬性都添加到目標對象(target參數(shù))。
如果只有一個參數(shù)提供給$.extend(),這意味著目標參數(shù)被省略。在這種情況下,jQuery對象本身被默認為目標對象。
目標對象(第一個參數(shù))將被修改,并且將通過$.extend()返回。然而,如果我們想保留原對象,我們可以通過傳遞一個空對象作為目標對象:
var obj= $.extend({}, obj1, obj2);
var obj1 = {
name: '11',
age: 22
};
var obj2 = {
name: '22',
sex: '男'
};
$.extend(obj1, obj2); //obj1 {name: "22", age: 22, sex: "男"} obj1改變
var obj1 = {
name: '11',
age: 22
};
var obj2 = {
name: '22',
sex: '男'
};
$.extend({}, obj1, obj2); // {name: "22", age: 22, sex: "男"}用這種方法,相當于把obj1和obj2合并后的結果賦值給一個新建的對象。不會改變obj1的對象結構。
jQuery 的鏈式調用是什么?
鏈式調用就是分步驟地對jQuery對象實現(xiàn)各種操作。
它的原理在于每一步的jQuery操作,返回的都是一個jQuery對象,所以不同操作可以連在一起。優(yōu)點在于:
1.代碼更精簡。鏈式調用能大大精簡代碼量,多項操作一行代碼一氣呵成;
2.優(yōu)化性能。使用鏈式調用,所有操作代碼共享一個jQuery對象,省去了逐步查詢DOM元素的性能損耗。
$this.parents('.mod-tab').find('.panel').eq(index).addClass('active').siblings().removeClass('active')
})
jQuery 中 data 函數(shù)的作用
存儲任意數(shù)據(jù)到指定的元素并且/或者返回設置的值。 jQuery.data() 方法允許我們在DOM元素上附加任意類型的數(shù)據(jù)。如果 DOM 元素是通過 jQuery 方法刪除的或者當用戶離開頁面時,jQuery 同時也會移除添加在上面的數(shù)據(jù)。我們可以在一個元素上設置不同的值,并獲取這些值:
$(node).data('a', 1) // key='a';value=1
$(node).data({b:2}) // 直接傳遞一個object
$(node).data() // {a:1,b:2}
寫出以下功能對應的 jQuery 方法:
$node = $('.node')
給元素 $node 添加 class active,給元素 $noed 刪除 class active
$node.addClass('active');
$node.removeClass('active');
展示元素$node, 隱藏元素$node
$node.show()
$node.hide()
獲取元素$node 的 屬性: id、src、title, 修改以上屬性
$node.attr('id')
$node.attr('id','value')
$node.attr('src')
$node.attr('src','value')
$node.attr('title')
$node.attr('title','value')
給$node 添加自定義屬性data-src
$node.attr('data-src','abc')
在$ct 內部最開頭添加元素$node
$ct.prepend($node)
在$ct 內部最末尾添加元素$node
$ct.append($node)
刪除$node
$node.remove()
把$ct里內容清空
$ctct.empty()
在$ct 里設置 html <div class="btn"></div>
$ct.html('<div class="btn"></div>')
獲取、設置$node 的寬度、高度(分別不包括內邊距、包括內邊距、包括邊框、包括外邊距)
//不包括內邊距
$node.height(); //讀取高度
$node.width(); //讀取寬度
$node.height('50px'); //設置高度
$node.width('50px'); //設置寬度
//包括內邊距
$node.innerHeight(); //讀取高度
$node.innerWidth(); //讀取寬度
$node.innerHeight( '50px' ); //設置高度
$node.innerHeight( '50px' ); //設置高度
//包括邊框
$node.outerHeight(); //讀取高度
$node.outerwidth(); //讀取寬度
$node.outerHeight('50px'); //設置高度
$node.outerwidth('50px'); //設置寬度
//包括外邊距
$node.outerHeight(true); //讀取高度
$node.outerwidth(true); //讀取寬度
獲取窗口滾動條垂直滾動距離
$node.scrollTop()
獲取$node 到根節(jié)點水平、垂直偏移距離
$node.offset()
修改$node 的樣式,字體顏色設置紅色,字體大小設置14px
$node.css({"color":"red","font-size":"14px"})
遍歷節(jié)點,把每個節(jié)點里面的文本內容重復一遍
$('#items li').each(function () {
var text = $(this).text();
$(this).text(text + text);
})
從$ct 里查找 class 為 .item的子元素
$ct.find('.item')
獲取$ct 里面的所有孩子
$ct.children();
對于$node,向上找到 class 為'.ct'的父親,在從該父親找到'.panel'的孩子
$node.parents('.ct').find('.panel');
獲取選擇元素的數(shù)量
$node.length
獲取當前元素在兄弟中的排行
$node.index()
用jQuery實現(xiàn)以下操作
當點擊$btn 時,讓 $btn 的背景色變?yōu)榧t色再變?yōu)樗{色
$btn.click(function(){
$btn.css('background','red')
setTimeout(function(){$btn.css('background','blue')},500)
})
當窗口滾動時,獲取垂直滾動距離
$(window).on('scroll',function(){
console.log($(window).scrollTop())
})
當鼠標放置到$div 上,把$div 背景色改為紅色,移出鼠標背景色變?yōu)榘咨?$div.on('mouseenter',function(){
$div.css({'background-color': 'red'})
})
$div.on('mouseout',function(){
$div.css({'background-color': 'white'})
})
當鼠標激活 input 輸入框時讓輸入框邊框變?yōu)樗{色,當輸入框內容改變時把輸入框里的文字小寫變?yōu)榇髮懀斴斎肟蚴ソ裹c時去掉邊框藍色,控制臺展示輸入框里的文字
$('input').on('input',function(){
var text = $(this).val().toUpperCase()
$(this).val(text)
})
$('input').blur(function(){
console.log($(this).val())
})
當選擇 select 后,獲取用戶選擇的內容
var $select = $('#select');
$select.on('change',function(){
console.log($select.val());
})
用 jQuery ajax 實現(xiàn)如下效果。`當點擊加載更多會加載數(shù)據(jù)展示到頁面
前端代碼
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
ul{
padding: 0;
}
li{
list-style: none;
border: 1px solid #ccc;
border-radius: 8px;
padding: 10px;
margin-top: 20px;
cursor: pointer;
}
li:hover{
background: green;
}
button{
display: block;
border-radius: 3px;
margin: 10px auto;
padding: 10px;
background: white;
color: #E21320;
border: 1px solid #e21320;
outline-color: transparent;
font-size: 16px;
cursor: pointer;
}
</style>
<body>
<ul>
<li>新聞1</li>
<li>新聞2</li>
</ul>
<button>加載</button>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
<script>
$('button').on('click',function () {
var index = $('ul li').length + 1;
$.ajax({
url: '/loadMore',
type: 'get',
data: {
index: index,
length: '5'
}
}).done(function (ret) {
appendHtml(ret);
}).fail(function () {
console.log('錯誤');
})
})
function appendHtml(news){
console.log(news);
var html = '';
$(news).each(function () {
html += '<li>'+this+'</li>';
});
$('ul').append(html);
}
</script>
</body>
</html>
后端
router.get('/loadMore',function (req,res) {
var index = req.query.index;
var length = req.query.length;
var data = [];
for(var i=0;i<length;i++){
data.push('內容' + (parseInt(index) + i));
}
res.send(data);
});