我爱编程

JQuery常见应用

2017-09-20  本文已影响0人  zh_yang

1、 jQuery 中, $(document).ready()是什么意思?

理想的页面加载方式:
解析HTML结构。
加载并解析外部脚本。
DOM树构建完成,执行脚本。//DOMInteractive –> DOMContentLoaded
加载图片、样式表文件等外部文件。
页面加载完毕。//window.onload

$(document).ready(handler)
$(handler)
$().ready(handler)
document.ready = handler  //JavaScript
$(document).load(handler)
window.onload = handler  //JavaScript写法

DOMLoading
浏览器开始解析dom树的时间点
DOMInteractive
表示浏览器已经解析好dom树了。
DOMContentLoaded
同步的JS已经执行完毕了。

JavaScript提供的,当初始HTML文档被完全加载和解析完成之后,DOMContentLoaded 事件被触发,而无需等待样式表、图像和子框架完成加载。效果和 $(document).ready()类似。

<script>
  document.addEventListener("DOMContentLoaded", function(event) {
      console.log("DOM fully loaded and parsed");
  });
</script>

2、 $node.html()和$node.text()的区别?

$(".box").html() //获取元素内部的html内容,类似于innerHTML
$(".box").html("<p>设置了一个段落</p>")//设置了元素内部的html内容,标签生效
$(".box").text() //获取元素内部的text文本,类似于innerText
$(".box").text("设置了一个文本")//设置了元素内部的text文本,标签不生效

3、 $.extend 的作用和用法?

var object=$.extend({},object1,object2);

如:

var obj1 = {
  name : 'John',
  age : 12,
  sex : 'man',
  class: {Chinese : 'classV',English : 'classII'}
}
var obj2 = {
  name : 'King',
  age : 22,
  sex : 'man',
  career: 'doctor', 
  class : {History : 'class1'}
}

var newObj = $.extend({},obj1,obj2)
console.log(newObj)
//{age:22,career:"doctor",class:{History: "class1"},name:"King",sex:"man"}

当一个参数为true时,将会进行递归的深拷贝合并。
用法为:

var object=$.extend(true,{},object1,object2);

更改上边的例子,变为:

var newObj = $.extend(true,{},obj1,obj2)
console.log(newObj)
//{age:22,career:"doctor",class:{Chinese: "classV", English: "classII", History: "class1"},name:"King",sex:"man"}

4、jQuery 的链式调用是什么?

return this

把对象再返回回来,对象当然可以继续调用方法啦,所以就可以链式操作了。

5、 jQuery 中 data 函数的作用

6、常见功能对应的 jQuery 方法

$node.addClass('active')
//添加
$node.removeClass('active')
//删除
$node.show()
//展示
$node.hide()
//隐藏
$node.attr('id/src/title','el') 
//只有第一个参数代表获取元素属性,第二个可选参数el为需要设置的属性
$node.attr('data-src','...')
//data-src值为'...'
$ct.prepend($node)
$node.prependTo($ct)
$ct.append($node)
$node.appendTo($ct)
$node.remove()
$ct.empty()
$ct.html('<div class="btn"></div>')
$node.width();//不包括内边距宽度,仅包括内容
$node.height();//不包括内边距高度,仅包括内容
$node.innerWidth();//包括内容和内边距宽度
$node.innerHeight();//包括内容和内边距高度
$node.outerWidth();//包括内容,内边距,边框宽度
$node.outerHeight();//包括内容,内边距,边框高度
$node.outerHeight(true);//包括内容,内边距,边框,外边距高度
$node.outerWidth(true);//包括内容,内边距,边框,外边距宽度
$(window).scrollTop()
$node.offset().left
$node.offset().top
$node.css({color : 'red',font-size : '14px'})
$.each($node,function(){
  console.log($(this).text());
});
或:
$node.each(function(){
  console.log($(this).text());
});
$ct.find('.item')
$ct.children()  //获取直接子元素
$ct.find('*')  //获取所有后代元素
$node.parents('.ct').find('.panel')
$node.length;
$node.size();
$(selector).index()

7、用jQuery实现以下操作

$("#btn").click(function(){
    $(this).css("background-color","red")
    setTimeout(function(){
      $("#btn").css("background-color","blue")
    },500)
  })
$(document).scroll(function(){
    console.log($(this).scrollTop()+'px')
  })
$('.div').on("mouseenter",function(){
    $(this).css("background","red");
});
$('.div').on("mouseleave",function(){
    $(this).css("background","#fff");
});
或:
$(".div").on("mouseover",function(){
    $(this).css("background-color","red");
});
$(".div").on("mouseout",function(){
    $(this).css("background-color","");
});
$('#ipt').on("focus",function(){
   $(this).css("outline-color","blue");
});
$('#ipt').on('keyup',function(){
    $(this).val($(this).val().toLocaleUpperCase());
});
$('#ipt').on('blur',function(){
     $(this).css("outline-color","none");
     if($(this).val()!=""){
         console.log($(this).val());
     }
});
$('#sel').on('change',function(){
    //或者console.log($(this).val());
    console.log($(this).find('option:selected').val());
});

8、用 jQuery ajax 实现加载更多

代码地址:https://github.com/zh-yang/resume/blob/master/task2-14/jq-clickmore/index-jq.html
server-mork如下:

920-1.gif

9、 实现一个天气预报页面

自己试着做了一下,不知道还有没有 bug (或者错误),改出来一些了。

代码地址:https://github.com/zh-yang/resume/tree/master/task2-14
预览地址:https://zh-yang.github.io/resume/task2-14/get-wearher/getWeatherII.html
预览截图:

920-2.gif
上一篇 下一篇

猜你喜欢

热点阅读