我爱编程

jQuery动画与ajax

2017-02-15  本文已影响39人  婷楼沐熙

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

为了防止文档在完全加载(就绪)之前运行jQuery代码,如果在文档没有加载之前就运行函数,可能操作失败。必须在文档加载完成后再执行操作,可使用ready事件,作用相当于把js写到body的末尾。

$(document).ready(function() {
   //your code here
})

$().ready(function() {
   $("p").hide();
})

$(function() {
  $('#btn').on('click', function() {
      $('p').show();
   })
})

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

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

jQuery.extend([deep,] target [, object1 ] [, objectN ] )
var obj = {};
var obj1 = {
   name: 'christina',
   sex: 'male'
};
var obj2 = {
  name: 'cttin',
  age: 18
}
$.extend(obj, obj1, obj2)  // 把obj1和obj2拷贝到obj,同名的将会被obj2覆盖
 $.extend({
  hello:function(){alert('hello');}
  });
 $.fn.extend({
  hello:function(){alert('hello');}
 });
var result=$.extend( true, {},
    { name: "John", location: {city: "Boston",county:"USA"} },  
    { last: "Resig", location: {state: "MA",county:"China"} } );
深拷贝
var result=$.extend( false, {},  
    { name: "John", location:{city: "Boston",county:"USA"} },  
    { last: "Resig", location: {state: "MA",county:"China"} }  
); 
浅拷贝

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

使用jQuery方法时对象方法返回的是对象本身,可以继续调用此对象的其他方法,实现连续调用多个方法。

$('#btn').on('click', function() {
    $('.main').fadeIn('slow').animate({height: '100px'}, 500);
})

五、jQuery 中 data 函数的作用

jQuery.data()允许我们在DOM元素上附加任意类型的数据,避免了循环引用的内存泄漏风险。

六、写出以下功能对应的 jQuery 方法:

$node.addClass('active');
$node.removeClass('active');
$node.show();
$node.hide();
// 获得
$node.attr('id');
$node.attr('src');
$node.attr('title');
// 修改
$node.attr('id', 'wrap');
$node.attr('src', 'https://github.com');
$node.attr('title', 'welcome');
$node.attr('data-src', wrap);
$ct.prepend($node);
$ct.append($node);
$node.remove()
$ct.empty();
$ct.html('<div class="btn"></div>')
$node.width(); // content
$node.height(); // content
$node.innerWidth(); // content+padding
$node.innerHeight(); // content+padding
$node.outWidth(); // content+padding+border
$node.outHeight(); // content+padding+border
$node.outWidth(true); // content+padding+border+margin
$node.outHeight(true); // content+padding+border+margin
$(window).scrollTop();
$node.offset();
$node.css({'color': 'red'; 'font-size': '14px'})
$.each(function () {
  console.log($(this).text());
})
$ct.find('.item');
$ct.children();
$node.parent('.ct').find('.panel');
$node.length;
$(this).index();

七、用jQuery实现以下操作:

八、用 jQuery ajax 实现如下效果。`当点击加载更多会加载数据展示到页面效果预览

这个之前已经写过,参见代码二

上一篇 下一篇

猜你喜欢

热点阅读