饥人谷技术博客

jQuery简单介绍

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

题目1: jQuery 能做什么?

题目2: jQuery 对象和 DOM 原生对象有什么区别?如何转化?

$('body')[0]//可以转为DOM原生对象
$(document.querySelector('body))//可以转为jQuery对象

题目3:jQuery中如何绑定事件?bind、unbind、delegate、live、on、off都有什么作用?推荐使用哪种?使用on绑定事件使用事件代理的写法?

$("p").bind("click", function(){
  alert( $(this).text() );
});
var foo = function () {
  // 处理某个事件的代码
};

$("p").bind("click", foo); // ... 当点击段落的时候会触发 foo 

$("p").unbind("click", foo); // ... 再也不会被触发 foo
<div style="background-color:red">
<p>这是一个段落。</p>
<button>请点击这里</button>
</div>

$("div").delegate("button","click",function(){
  $("p").slideToggle();
});
$("p").live("click", function(){
    $(this).after("<p>Another paragraph!</p>");
});
function myHandler(event) {
alert(event.data.foo);
}
$("p").on("click", {foo: "bar"}, myHandler)
var validate = function () {
  // code to validate form entries
};
// delegate events under the ".validator" namespace
$("form").on("click.validator", "button", validate);
$("form").on("keypress.validator", "input[type='text']", validate); 
// remove event handlers in the ".validator" namespace
$("form").off(".validator");

推荐使用.on()、.off()

$('ul').on('click','li',function(){
console.log()this
})

题目4:jQuery 如何展示/隐藏元素?题目5: jQuery 动画如何使用?

 <style>
    .box {
      width: 100px;
      height: 100px;
      background-color: red;
      position: relative;
    }
    ul>li {
      list-style: none;
      border: 1px solid;
    }
     p {
       display: none;
     }
  </style>
  <div class="box"></div>
  <ul>
    <li>
      <h1>我是标题1</h1>
      <p>我是段落1</p>
    </li>
    <li>
      <h1>我是标题2</h1>
      <p>我是段落2</p>
    </li>
    <li>
      <h1>我是标题3</h1>
      <p>我是段落3</p>
    </li>
  </ul>
  <button id="show">显示</button>
  <button id="hide">隐藏</button>
  <button id="more">多个动画</button>
  <button id="finish">完成</button>
  <button id="stop">停止</button>
  <script>
    var $box = $('.box')
    $('#show').on('click', function () {
      $box.show(1000, function () {
        $(this).text('animation is done')
      })
    })
    $('#hide').on('click', function () {
      $box.hide(1000)
    })
    $('ul').on('click','li',function () {
      $(this).find('p').toggle(1000)//动态效果为从右至左。横向动作。
      $(this).find('p').slideToggle(1000)//动态效果为从下至上。竖向动作。
      $(this).find('p').slideUp(1000)//通过高度变化(向上减小)来动态地隐藏示所有匹配的元素
      $(this).siblings().find('p').slideDown(1000)//通过高度变化(向下增大)来动态地显示所有匹配的元素
      $(this).find('p').fadeIn(1000)//通过不透明度的变化来实现所有匹配元素的淡入效果
      $(this).find('p').fadeOut(1000)//通过不透明度的变化来实现所有匹配元素的淡出效果
      $(this).find('p').fadeToggle(1000)//通过不透明度的变化来开关所有匹配元素的淡入和淡出效果
      $(this).find('p').fadeTo(1000,0.36)//把所有匹配元素的不透明度从0以渐进方式调整到指定的不透明度
    })
    $('#more').on('click',function(){
      $box.animate({width:'140px',height:'140px',left:'300px'},1000)
      $box.animate({top:'300px'},1000).animate({left:'0'}).animate({top:'0',width:'100px',height:'100px'})
    })

    $('#finish').on('click',function(){
      $box.finish()
    })
    $('#stop').on('click',function(){
      $box.stop(true)
    })
  </script>

题目6:如何设置和获取元素内部 HTML 内容?如何设置和获取元素内部文本?

    <ul>
        <li id="li01">
          <h1>我是标题1</h1>
          <p>我是段落1</p>
        </li>
        <li id="li02">
          <h1>我是标题2</h1>
          <p>我是段落2</p>
        </li>
        <li id="li03">
          <h1>我是标题3</h1>
          <p>我是段落3</p>
        </li>
      </ul>
      <script>
        console.log($('ul').html())//获取HTML内容
        $('ul #li01').html('<div>hello jirengu</div>')//设置HTML内容
          console.log($('ul #li02').text())//获取文本内容
          $('ul #li02').text('hello world')//设置文本内容
      </script>

题目7:如何设置和获取表单用户输入或者选择的内容?如何设置和获取元素属性?

    console.log($("#username").val())//获取input的值
    $("#username").val('jirengu')//设置input的值
    console.log($("#username").attr('type'))//获取input的type属性
    $("#username").attr('type', 'password')//设置input的type属性值为'password
    $('#city').change(function () {
      console.log($(this).val())//获取select选择时的value
    })

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

sideNav
题目9
题目10
题目10 server.js
tab切换卡

上一篇 下一篇

猜你喜欢

热点阅读