題目1: jQuery 中, $(document).ready()是什么意思?
- ready( handler )
當DOM準備就緒時,指定一個函數來執行。
雖然JavaScript提供了load事件,當頁面呈現時用來執行這個事件,直到所有的東西,如圖像已被完全接收前,此事件不會被觸發。
在大多數情況下,只要DOM結構已完全加載時,腳本就可以運行。傳遞處理函數給.ready()方法,能保證DOM準備好后就執行這個函數,因此,這里是進行所有其它事件綁定及運行其它 jQuery 代碼的最佳地方。
如果執行的代碼需要在元素被加載之后才能使用時,(例如,取得圖片的大小需要在圖片被加載完后才能知道),就需要將這樣的代碼放到 load 事件中。
下面兩種語法全部是等價的:
$(document).ready(handler)
$(handler)
我們經常這么使用
$(function(){
console.log('ready');
});
題目2: $node.html()和$node.text()的區別?
html([string])
這是一個讀寫兩用的方法,用于獲取/修改元素的innerHTML
當沒有傳遞參數的時候,返回元素的innerHTML
當傳遞了一個string參數的時候,修改元素的innerHTML為參數值
看個例子
$('div').html()
$('div').html('123')
后續這種讀寫兩用的方法很多,原理都類似
如果結果是多個進行賦值操作的時候會給每個結果都賦值
如果結果多個,獲取值的時候,返回結果集中的第一個對象的相應值
text()
和html方法類似,操作的是DOM的innerText值
題目3: $.extend 的作用和用法?
- 作用:可以對用戶輸入的參數設置默認(缺省)值;對對象進行深/淺拷貝(后面的對象的相同屬性會覆蓋)
- 用法:
let obj1 = {},
obj2 = {
name: 'cl',
age: '12'
},
obj3 = {
name: 'tom',
sex: 'male'
},
obj4 = {
name: 'lily',
hobby: 'book'
};
$.extend(obj1, obj2, obj4); //用法1:改變obj1的內容,依次讓obj2、obj3覆蓋obj1,生成新的obj1
console.log(obj1); //{name: "lily", age: "12", hobby: "book"}
obj1 = $.extend({}, obj2, obj3, obj4); //用法2:生成一個新的對象,依次讓obj2、obj3、obj4覆蓋空對象,生成新的對象
console.log(obj1); //{name: "lily", age: "12", sex: "male", hobby: "book"}
$.fn.extend(obj2); //用法3:給所有jquery對象加上obj2的屬性和值(相當于jquery對象加了靜態屬性)
console.log($(window).name, $(window).age); //cl 12
題目4: jQuery 的鏈式調用是什么?
對于同一個jquery對象,可以使用.css('color','blue').attr('alt','..')這種方式簡化代碼
//1.不使用鏈式調用
$box.css('background-color','blue');
$box.css('border-bottom','3px dotted green');
$box.addClass('box1');
//2.使用鏈式調用
$box.css('background-color', 'blue').css('border-bottom', '3px dotted green').addClass('box1');
//========可以簡化代碼
$box.css({
'background-color': 'blue',
'border-bottom': '3px dotted green'
}).addClass('box1');
題目5: jQuery 中 data 函數的作用
- 定義:在匹配元素上存儲任意相關數據 或 返回匹配的元素集合中的第一個元素的給定名稱的數據存儲的值。
- 特點:通過data()函數存取的數據都是臨時數據,一旦頁面刷新,之前存放的數據都將被移除。
- 作用:
允許我們在DOM元素上綁定任意類型的數據,避免了循環引用的內存泄漏風險。。
題目6:寫出以下功能對應的 jQuery 方法:
給元素 $node 添加 class active,給元素 $noed 刪除 class active
$node.addClass('active');
$node.removeClass('active');
展示元素$node, 隱藏元素$node
$node.show();
$node.hide();
獲取元素$node 的 屬性: id、src、title, 修改以上屬性
let id = $node.attr('id'), src=$node.attr('src'), title=$node('title');//獲取
$node.attr('id', 'special').attr('src', './img/g.png').attr('title', 'river');//修改
給$node 添加自定義屬性data-src
$node.attr('data-src','./img/m.png');
在$ct 內部最開頭添加元素$node
$ct.prepend($node);
在$ct 內部最末尾添加元素$node
$ct.append($node);
刪除$node
$ct.remove($node);//刪除界面上的元素,同時刪除元素上綁定的事件
$ct.detach($node);//和remove類似,但保留元素上綁定的事件
把$ct里內容清空
$ct.empty();
在$ct 里設置 html <div class="btn"></div>
$ct.html('<div class="btn"></div>');
獲取、設置$node 的寬度、高度(分別不包括內邊距、包括內邊距、包括邊框、包括外邊距)
- 獲取$node 的寬度、高度
//不包括內邊距
$node.width()
$node.height()
//包括內邊距
$node.innerWidth()
$node.innerHeight()
//包括內邊距、包括邊框
$node.outerWidth()
$node.outerHeight()
//包括內邊距、包括邊框、包括外邊距
$node.outerWidth(true)
$node.outerHeight(true)
- 設置$node 的寬度、高度
//不包括內邊距
$node.width('100px');
$node.height('100px');
//包括內邊距
$node.innerWidth('100px');
$node.innerHeight('100px');
//包括內邊距、包括邊框
$node.outerWidth('100px');
$node.outerHeight('100px');
//包括內邊距、包括邊框、包括外邊距
$node.outerWidth('100px', true);
$node.outerHeight('100px', true);
獲取窗口滾動條垂直滾動距離
let offsetY = $(window).scrollTop(或者window.scrollY)
獲取$node 到根節點水平、垂直偏移距離
$node.offset().left//水平
$node.offset().top//垂直
修改$node 的樣式,字體顏色設置紅色,字體大小設置14px
$node.css({color:'red', 'font-size':'14px'})
遍歷節點,把每個節點里面的文本內容重復一遍
$node.each(function(){
console.log($(this).text());
})
從$ct 里查找 class 為 .item的子元素
//只查找直接子元素:
$ct.children('.item');
//查找所有后代元素
$ct.find('.item');
獲取$ct 里面的所有孩子
$ct.children()
對于$node,向上找到 class 為'.ct'的父親,在從該父親找到'.panel'的孩子
$node.parent('.ct').find('.panel')
獲取選擇元素的數量
.length
獲取當前元素在兄弟中的排行
.index
題目7:
用jQuery實現以下操作當點擊$btn 時,讓 $btn 的背景色變為紅色再變為藍色
$btn.on('click', function(){
$btn.css('backgroud-color', 'red');
$btn.css('backgroud-color', 'blue');
});
當窗口滾動時,獲取垂直滾動距離
$(window).scrollTop();
當鼠標放置到$div 上,把$div 背景色改為紅色,移出鼠標背景色變為白色
$div .on('mouseenter',function(){
$(this).css('background-color','red');
});
$div .on('mouseleave',function(){
$(this).css('background-color','white');
});
//或
$('button').mouseenter(function () {
$(this).css('background-color', 'red');
});
$('button').mouseleave(function () {
$(this).css('background-color', 'white');
});
當鼠標激活 input 輸入框時讓輸入框邊框變為藍色,當輸入框內容改變時把輸入框里的文字小寫變為大寫,當輸入框失去焦點時去掉邊框藍色,控制臺展示輸入框里的文字
當選擇 select 后,獲取用戶選擇的內容
$('input').on('focusin', function () {
$(this).css('border-color', 'blue');
});
$('input').on('focusout', function () {
$(this).css('border-color', 'white');
});
//或
$('input').focusin(function () {
$(this).css('border-color', 'blue');
});
$('input').focusout(function () {
$(this).css('border-color', 'white');
});
當選擇 select 后,獲取用戶選擇的內容
當選擇 select 后,獲取用戶選擇的內容
題目8: 用 jQuery ajax 實現如下效果。`當點擊加載更多會加載數據展示到頁面效果預覽352
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>加載更多</title>
<style>
ul,li{
margin: 0;
padding: 0
}
#ct li{
list-style:none;
border: 1px solid #ccc;
padding: 10px;
margin-top: 10px;
cursor:pointer;
}
#ct li:hover{
background: red;
color: #fff;
}
#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{
background: green;
color: #fff;
}
</style>
<script type="text/javascript">
window.onload = function (){
var btn = document.querySelector('#load-more');
var ct = document.querySelector('#ct');
var pageIndex = 0;
var isDataArray = true; //防止數據到來之前重復點擊*/
btn.addEventListener('click',function (){
if(!isDataArray){
return;
}
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status === 200 || xhr.statue=== 304){
var results = JSON.parse(xhr.responseText);
console.log(results);
var fragment = document.createDocumentFragment();
for(var i=0; i<results.length; i++){
var node = document.createElement('li');
node.innerText = results[i];
fragment.appendChild(node);
};
ct.appendChild(fragment);
pageIndex = pageIndex + 5;
}else{
alert(出錯了);
};
isDataArray = true;
};
};
xhr.open('get', '/loadMore?index='+pageIndex+'&length=5', true);
xhr.send();
isDataArray = false;
});
}
</script>
</head>
<body>
<ul id="ct">
</ul>
<a id="load-more" class="btn" href="javascript:void(0)">
加載更多
</a>
</body>
</html>
app.get('/loadMore',function(req, res) {
var curIdx = req.query.index;
var len = req.query.length;
var data = [];
for(var i = 0; i < len; i++) {
data.push('新聞' + (parseInt(curIdx) + i))
}
res.send(data);
});