題目1: jQuery 中, $(document).ready()是什么意思?
- $(document).ready(handler)與$(handler)作用相同
- 作用:讓document內(nèi)的所有元素加載完成(不包括圖片)后,再加載js方法(handler)
- jquery的ready和原生onload的區(qū)別
題目2: $node.html()和$node.text()的區(qū)別?
- 前者是對jquery對象的html內(nèi)容進(jìn)行操作(獲取/添加/修改)
$('div.wrapper').html('<h1>hello world</h1>')
- 后者是對jquery對象的文本內(nèi)容進(jìn)行操作(獲取/添加/修改)
$('div.wrapper').text('hello world')
題目3: $.extend 的作用和用法?
- 作用:可以對用戶輸入的參數(shù)設(shè)置默認(rèn)(缺?。┲担粚ο筮M(jìn)行深/淺拷貝(后面的對象的相同屬性會(huì)覆蓋)
- 用法:
let obj1 = {},
obj2 = {
name: 'cl',
age: '12'
},
obj3 = {
name: 'tom',
sex: 'male'
},
obj4 = {
name: 'lily',
hobby: 'book'
};
$.extend(obj1, obj2, obj4); //用法1:改變obj1的內(nèi)容,依次讓obj2、obj3覆蓋obj1,生成新的obj1
console.log(obj1); //{name: "lily", age: "12", hobby: "book"}
obj1 = $.extend({}, obj2, obj3, obj4); //用法2:生成一個(gè)新的對象,依次讓obj2、obj3、obj4覆蓋空對象,生成新的對象
console.log(obj1); //{name: "lily", age: "12", sex: "male", hobby: "book"}
$.fn.extend(obj2); //用法3:給所有jquery對象加上obj2的屬性和值(相當(dāng)于jquery對象加了靜態(tài)屬性)
console.log($(window).name, $(window).age); //cl 12
題目4: jQuery 的鏈?zhǔn)秸{(diào)用是什么?
對于同一個(gè)jquery對象,可以使用.css('color','blue').attr('alt','..')這種方式簡化代碼
//1.不使用鏈?zhǔn)秸{(diào)用
$box.css('background-color','blue');
$box.css('border-bottom','3px dotted green');
$box.addClass('box1');
//2.使用鏈?zhǔn)秸{(diào)用
$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 函數(shù)的作用
- 定義:在匹配元素上存儲(chǔ)任意相關(guān)數(shù)據(jù) 或 返回匹配的元素集合中的第一個(gè)元素的給定名稱的數(shù)據(jù)存儲(chǔ)的值。
- 特點(diǎn):通過data()函數(shù)存取的數(shù)據(jù)都是臨時(shí)數(shù)據(jù),一旦頁面刷新,之前存放的數(shù)據(jù)都將被移除。
- 作用:
允許我們在DOM元素上綁定任意類型的數(shù)據(jù),避免了循環(huán)引用的內(nèi)存泄漏風(fēng)險(xiǎn)。。
//存放數(shù)據(jù)
$("body").data("foo", 52);
$("body").data("bar", { myType: "test", count: 40 });
$("body").data({ baz: [ 1, 2, 3 ] });
//獲取數(shù)據(jù)
$("body").data("foo"); // 52
$("body").data(); // { foo: 52, bar: { myType: "test", count: 40 }, baz: [ 1, 2, 3 ] }
題目6:寫出以下功能對應(yīng)的 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 內(nèi)部最開頭添加元素$node
$ct.prepend($node);
- 在$ct 內(nèi)部最末尾添加元素$node
$ct.append($node);
- 刪除$node
$ct.remove($node);//刪除界面上的元素,同時(shí)刪除元素上綁定的事件
$ct.detach($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)邊距、包括邊框、包括外邊距)
- 獲取$node 的寬度、高度
//不包括內(nèi)邊距
$node.width()
$node.height()
//包括內(nèi)邊距
$node.innerWidth()
$node.innerHeight()
//包括內(nèi)邊距、包括邊框
$node.outerWidth()
$node.outerHeight()
//包括內(nèi)邊距、包括邊框、包括外邊距
$node.outerWidth(true)
$node.outerHeight(true)
- 設(shè)置$node 的寬度、高度
//不包括內(nèi)邊距
$node.width('100px');
$node.height('100px');
//包括內(nèi)邊距
$node.innerWidth('100px');
$node.innerHeight('100px');
//包括內(nèi)邊距、包括邊框
$node.outerWidth('100px');
$node.outerHeight('100px');
//包括內(nèi)邊距、包括邊框、包括外邊距
$node.outerWidth('100px', true);
$node.outerHeight('100px', true);
- 獲取窗口滾動(dòng)條垂直滾動(dòng)距離
let offsetY = $(window).scrollTop(或者window.scrollY)
- 獲取$node 到根節(jié)點(diǎn)水平、垂直偏移距離
$node.offset().left//水平
$node.offset().top//垂直
- 修改$node 的樣式,字體顏色設(shè)置紅色,字體大小設(shè)置14px
$node.css({color:'red', 'font-size':'14px'})
- 遍歷節(jié)點(diǎn),把每個(gè)節(jié)點(diǎn)里面的文本內(nèi)容重復(fù)一遍
$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')
- 獲取選擇元素的數(shù)量
.length
- 獲取當(dāng)前元素在兄弟中的排行
.index
題目7:用jQuery實(shí)現(xiàn)以下操作
- 當(dāng)點(diǎn)擊$btn 時(shí),讓 $btn 的背景色變?yōu)榧t色再變?yōu)樗{(lán)色
$btn.on('click', function(){
$btn.css('backgroud-color', 'red');
$btn.css('backgroud-color', 'blue');
});
- 當(dāng)窗口滾動(dòng)時(shí),獲取垂直滾動(dòng)距離
$(window).scrollTop();
- 當(dāng)鼠標(biāo)放置到$div 上,把$div 背景色改為紅色,移出鼠標(biāo)背景色變?yōu)榘咨?/li>
$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');
});
- 當(dāng)鼠標(biāo)激活 input 輸入框時(shí)讓輸入框邊框變?yōu)樗{(lán)色,當(dāng)輸入框內(nèi)容改變時(shí)把輸入框里的文字小寫變?yōu)榇髮?,?dāng)輸入框失去焦點(diǎn)時(shí)去掉邊框藍(lán)色,控制臺展示輸入框里的文字
$('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');
});
- 當(dāng)選擇 select 后,獲取用戶選擇的內(nèi)容
$('select').change(function(){
console.log($(this).val());
});
題目8: 用 jQuery ajax 實(shí)現(xiàn)如下效果。當(dāng)點(diǎn)擊加載更多會(huì)加載數(shù)據(jù)展示到頁面效果預(yù)覽14
-
代碼鏈接
- entry.js:jQ和ajax代碼,包括按鈕事件、引用scss樣式、渲染List組件到react的虛擬dom
- index.js:創(chuàng)建react的List列表組件和item組件,輸出List組件
- index.scss:樣式
- bundle.js:webpack打包js文件(壓縮代碼),包括所有樣式、react組件和js代碼
- index.html:主界面,引入bundel.js
- 服務(wù)端代碼鏈接