饥人谷技术博客

jQuery动画与ajax

2017-10-29  本文已影响0人  饥人谷_Jack

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

简单来说 window.onload是当页面呈现时用来执行这个事件,直到所有的东西,如图像已被完全接收前,此事件不会被触发
$(document).ready()只要DOM结构已完全加载时,脚本就可以运行。传递处理函数给.ready()方法,能保证DOM准备好后就执行这个函数

题目2: $node.html()和$node.text()的区别?

题目3: $.extend 的作用和用法?

    var obj1 = {a:1,b:2}
    var obj2 = {a:1,b:2,c:3}
    var obj3 = {}
    var obj4 = {d:4,e:5}
    $.extend(obj3,obj1,obj2)
    var obj5 = $.extend({},obj2,obj4)
    console.log(obj3)
    console.log(obj5)

题目4: jQuery 的链式调用是什么?

题目5: jQuery 中 data 函数的作用

$("div").data("blah");  // undefined
$("div").data("blah", "hello");  // blah设置为hello
$("div").data("blah");  // hello
$("div").data("blah", 86);  // 设置为86
$("div").data("blah");  //  86
$("div").removeData("blah");  //移除blah
$("div").data("blah");  // undefined

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

$node.addClass('active')
$node.removeClass('active')
$node.show()
$node.hide()
$node.attr('id')
$node.attr('id','value')
$node.attr('src')
$node.attr('id','value')
$node.attr('title')
$node.attr('id','value')
$node.attr('data-src','http://aaa.com')
$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()
$node.css({color:'red','font-size':'14px'})
    $('div').each(function(){
      var str = $(this).text()
      $(this).text(str + str)
    })
$ct.find('.item')
$ct.find('*')
$node.parents('.ct').find('.panel')
$node.length
$node.index()

题目7:用jQuery实现以下操作

    $('button').on('click',function() {
      $('.ct').css('background-color',"red")
      setTimeout(function(){
        $('.ct').css('background-color',"blue")
      },600)
    })
    $(window).on('scroll',function(){
      $('.ct').text($(window).scrollTop())
    })
   $('.ct').on('mouseenter',function(){
     $(this).css('background-color','red')
   })
   $('.ct').on('mouseleave',function(){
     $(this).css('background-color','white')
   })
  $('input').on('focus',function(){
    $('input').css('border-color','blue')
  })
  $('input').on('blur',function(){
    $('input').css('border','')
  })
  $('input').on('change',function(){
    var $val = $('input').val()
    console.log($val.toUpperCase())
  })
    $('#city').on('change',function(e){
      console.log($('#city option:selected').text())

题目8: 用 jQuery ajax 实现如下效果。

 <div class="wrap">
    <ul>
      <li>内容1</li>
      <li>内容2</li>
    </ul>
  </div>
  <div class="btn">
    <button>加载更多</button>
  </div>

  <script>
    var pageIndex = 3;
    $('.btn button').on('click', function () {
      $.ajax({
        url: '/loadMore',
        type: 'get',
        dataType: 'json',
        data: { index: pageIndex, length: 5 }
      }).done(function (data) {
        add(data)
        console.log(data)
      }).fail(function () {
        console.log('失败')
      })
      pageIndex += 5;
    })
    function add(str) {
      for (var i = 0; i < str.length; i++) {
        var html = ''
        html += '<li>' + str[i] + '</li>'
        $('.wrap>ul').append(html)
      }
    }
router.get('/loadMore', function(req, res) {
  var pageIndex = req.query.index
  var len = req.query.length
  var data = []
  for (var i = 0; i < len; i++) {
    data.push('内容' + (parseInt(pageIndex) + i))
  }
  res.end(JSON.stringify(data))
})

上一篇 下一篇

猜你喜欢

热点阅读