jquery常见面试题

2,052 阅读1分钟

1:JQuery的复杂逻辑:和.fn的区别

  • $是JQuery这个构造函数 对外提供一个简写的写法。
  • .fn 中的fn是什么意思,其实是prototype,即.fn=$.prototype;他用来扩展JQuery方法

2:基础JQuery扩展一个方法?

1: .extend方法扩展工具方法  类似.ajax 、.trim 、.each
$.extend({
	add:function(str){
		return str+' jquery';
	}
})

console.log($.add('hello'));   //输出就是hello jquery
2: .fn.extend和.fn方法都是给$("") 获取dom对象扩展的方法
//$.fn扩展方法
$.fn.delte = function(){
	$(this).remove();
}
$.fn.extend({
	delte:function(){
		$(this).remove();
	}
})
$(".J_div").delte();

3:attr和prop区别是什么?


<input id="chk1" type="checkbox" />是否可见
<input id="chk2" type="checkbox" checked="checked" />是否可见

<!--attr 一般用来获取设置元素自定义属性  这个时候attr是获取不到-->
$("#chk1").attr("checked") == undefined
$("#chk2").attr("checked") == "checked"

<!--prop    一般用来获取设置元素固定自带的属性  可以获取到-->

$("#chk1").prop("checked") == false
$("#chk2").prop("checked") == true

4:jquery中哪个方法可以同时请求多个ajax并处理?

when方法

	$.when(
		$.ajax({
			type:'get',
			url:"https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=web前端",	
			dataType: 'jsonp',
		    jsonp: 'cb',
		}),
		$.ajax({
			type:'get',
			url:"https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=百度",	
			dataType: 'jsonp',
		    jsonp: 'cb',
		})
		
	).done(function(a1,a2){
	    //al 代表第一个接口请求返回成功的对象集合
		console.log(a1);
		//a2 代表第二个接口请求返回成功的对象集合
		console.log(a2);
	});

5:JQuery绑定事件有哪些方法?

  • bind 可以直接绑定事件,也可以通过事件委托形势给子元素事件
	//bind事件委托	发现绑定给子元素  点击父元素也会触发事件  
	$('div').bind('click','span',function() {
	    //this是指向的div
		alert($(this).text());
	});
  • on 可以直接绑定事件,也可以通过事件委托形势给子元素事件
//on事件委托	发现绑定给子元素 点击父元素不会触发事件
    $('div').on('click','span',function() {
        //this指向的span
		alert($(this).text());
	});
  • live 把事件绑定给了document。利用事件委托原理处理事件 1.9版本已经删除掉了
  • delegate 只能用事件委托形式,不能直接绑定事件
$('div').delegate('span','click',function() {
    //this指向的span
	alert($(this).text());
});