我爱编程

jQuery ajax

2018-01-04  本文已影响0人  1w1ng

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

如果执行的代码需要在元素被加载之后才能使用时,(例如,取得图片的大小需要在图片被加载完后才能知道),就需要将这样的代码放到 load 事件中。

$(document).ready(handler)
$(handler)

$(function(){
  console.log('ready');
});

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

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

$.extend 的作用和用法?

jQuery.extend( target [, object1 ] [, objectN ] )

var obj1 = {name:'wing'}
var obj2 = {age:19}
var obj3 = {name:'hcb',age:20}
var obj4 = {}
var obj5 = $.extend(obj4,obj1,obj2,obj3) //{name: "hcb", age: 20}

jQuery 的链式调用是什么?

jQuery 中 data 函数的作用

data([key],[value])
在元素上存放或读取数据,返回jQuery对象。

例子:
$("body").data("foo" , 18);
$("body").data("abc", { name: "text", sex: 20 });
$("body").data({cba:[a,b,c]});
$("body").data("foo"); // 18
$("body").data() // {foo: 18, abc: {name: "text", sex: 20}, cba:[a,b,c]}

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

$node.addClass('active') //添加
$noed.removeClass('active') //删除
$node.show(); //展示
$node.hide(); //隐藏
//获取并修改
$node.attr('id','newId');
$node.attr('src','newSrc');
$node.attr('title','newTitle');
$node.attr("data-src","value");
$ct.prepend($node);
$ct.append($node);
$node.remove();
$ct.empty();
$ct.html('<div class="btn"></div>');
//不包括内边距
$node.width();
$node.height();
//包括内边距
$node.innerWidth();
$node.innerHeight();
//包括内边距、包括边框
$node.outerWidth();
$node.outerHeight();
//包括内边距、包括边框、包括外边距
$node.outerWidth(true);
$node.outerHeight(true);
$(window).scrollTop();
$node.offset().left; //水平
$node.offset().top;   //垂直
$node.css({color:'red',  'font-size':'14px'});
$node.each(function(){
  console.log($(this).text());
});

$ct.find('.item');
$ct.children();
$node.parents('.ct').find('.panel');
$node.length;
$node.index();

用jQuery实现以下操作

$("#btn").on("click",function(){
  $(this).css("background-color":"red")
//设置定时
  setTimeout(function(){
    $("#btn").css("background-color":"blue")
  },1000)
})
$(window).on("scroll",function(){
  $("node").text($(window).scrollTop()+"px");
});
$btn.on('mouseenter',function(){
  $(this).css('background','red')
}).on('mouseleave',function(){
  $(this).css('background','white')
})
<input id="ipt" type="text">

$("#ipt").on("focus",function(){
  $(this).css({"outlineColor":"blue"})
})
//键盘事件
$("#ipt").on("keydown",function(){
  $(this).val($(this).val().toUpperCase())
})
$("#ipt").on("blur",function(){
  $(this).css({"outlineColor":"none"})
  console.log($(this).val())
})
$('#city').change(function(){
  var $val = $(this).val()
  $('.user-select').text('选择:' + $val)
})

用 jQuery ajax 实现效果。当点击加载更多会加载数据展示到页面

mock start

代码地址

上一篇 下一篇

猜你喜欢

热点阅读