美文网首页
进阶任务十五-jQuery动画与Ajax

进阶任务十五-jQuery动画与Ajax

作者: RookieD | 来源:发表于2017-08-30 16:12 被阅读0次

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

当DOM准备就绪的时候,指定一个函数执行

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

  • $node.html()获取集合中第一个匹配元素的HTML内容或设置每一个匹配元素的html内容
  • $node.text()得到匹配元素集合中每个元素的文本内容结合,包括他们的后代,或设置匹配元素集合中每个元素的文本内容为指定的文本内容

$.extend 的作用和用法?

  • 作用:将两个或更多对象的内容合并到第一个对象
  • 用法:
var obj1 = {a:1,b:2}
var obj2 = {b:3,c:4}
var obj = $.extend({}, obj1, obj2)

jQuery 的链式调用是什么?

jQuery的方法都会返回调用方法的对象本身,所以对象可以连续调用多个jQuery方法,好处节省代码量,代码看起来更优雅、
返回的都是同一个对象,可以提高代码的效率、让代码流程更清晰

jQuery 中 data 函数的作用

在匹配元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值

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

  • 给元素 $node 添加 class active,给元素 $noed 删除 class active
    $node.addClass("active") $node.removeClass("active")
  • 展示元素$node, 隐藏元素$node
    $node.show() $node.hide()
  • 获取元素$node 的 属性: id、src、title, 修改以上属性
    $node.attr("id") $node.attr("id","content")
    $node.attr("src") $node.attr("src","content")
    $node.attr("title") $node.attr("title","content")
  • 给$node 添加自定义属性data-src
    $node.attr("data-src","content")
  • 在$ct 内部最开头添加元素$node
    $ct.prepend($node)
  • 在$ct 内部最末尾添加元素$node
    $ct.append($node)
  • 删除$node
    $node.remove()
  • 把$ct里内容清空
    $ct.empty()
  • 在$ct 里设置 html <div class="btn"></div>
    $ct.html('html <div class="btn"></div>')
  • 获取、设置$node 的宽度、高度(分别不包括内边距、包括内边距、包括边框、包括外边距)
$node.width();
$node.height();
//不包括内边距

$node.innerWidth();
$node.innerHeight();
//包括内边距

$node.outerWidth();
$node.outerHeight();
//包括边框

$node.outerWidth(true);
$node.outerHeight(true);
//包括外边距

设置:
$node.width('100px');
$node.height('100px');
//不包括内边距

$node.innerWidth('100px');
$node.innerHeight('100px');
//包括内边距

$node.outerWidth('100px');
$node.outerHeight('100px');
//包括边框

$node.outerWidth('100px',true);
$node.outerHeight('100px',true);
//包括外边距
  • 获取窗口滚动条垂直滚动距离
    $(window).scrollTop()
  • 获取$node 到根节点水平、垂直偏移距离
    $node.offset().left
    $node.offset().top
  • 修改$node 的样式,字体颜色设置红色,字体大小设置14px
    $node.css({"color":"red","font-size":"14px"})
  • 遍历节点,把每个节点里面的文本内容重复一遍
$node.each(function(){
    $(this).text()+$(this).text();
});
  • 从$ct 里查
    $ct.find()

用jQuery实现以下操作

  • 当点击$btn 时,让 $btn 的背景色变为红色再变为蓝色
    http://js.jirengu.com/corew/4/edit

  • 当窗口滚动时,获取垂直滚动距离
    http://js.jirengu.com/sijuf/2/edit

  • 当鼠标放置到$div 上,把$div 背景色改为红色,移出鼠标背景色变为白色
    http://js.jirengu.com/zovoy/4/edit

  • 当鼠标激活 input 输入框时让输入框边框变为蓝色,当输入框内容改变时把输入框里的文字小写变为大写,当输入框失去焦点时去掉边框蓝色,控制台展示输入框里的文字

<!doctype html>
<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <style>
        .focus {
            border-color: blue;
        }
    </style>
    <body>
        <input type="text">
    </body>
    <script>
        $("input").on("focus", function(){
            $(this).addClass("focus")
        }).on("keyup", function(){
            $(this).val($(this).val().toUpperCase())
        }).on("blur", function(){
            $(this).removeClass("focus")
            console.log($(this).val())
        })
    </script>
</html>

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

前端代码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <!--<script src="../jquery-3.2.1.min.js"></script>-->
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }
    #ct li {
        border: 1px solid #ccc;
        padding: 10px;
        margin-top: 10px;
        margin-left: 8px;
        margin-right: 8px;
        cursor: pointer;
    }
    #load-more {
        display: block;
        margin: 10px auto;
        text-align: center;
        cursor: pointer;
    }
    #load-more img {
        width: 40px;
        height: 40px;
    }
    .btn {
        display: inline-block;
        height: 40px;
        line-height: 40px;
        width: 80px;
        border: 1px solid #e27272;
        border-radius: 3px;
        text-align: center;
        text-decoration: none;
        color: #e27272;
    }
    .btn:hover,li:hover {
        background: green;
        color: #fff;
    }
</style>
<body>
<ul id="ct">
    <li>内容1</li>
    <li>内容2</li>
</ul>
<a href="javascript:void(0)" id="load-more" class="btn">加载更多</a>

<script>
    var currentIndex = 2;
    var currentLength = 5;

    $('#load-more').on('click',function () {
        $.ajax({
            url: '/loadMore',
            method: 'GET',
            data: {
                index: currentIndex,
                length: currentLength,
            }
        }).done(function (ret) {
            render(ret);
            currentIndex += currentLength;
        })
    })
    function render (data) {
        var html = '';
        var $data = $(data);
        $data.each(function (i,e) {
            html += '<li>' + e + '</li>';
        })
        $('#ct').append(html);
    }
</script>
</body>
</html>

后端代码:

app.get('/loadMore',function (req,res) {
    var index = req.query.index;
    var length = req.query.length;
    var data = [];
    for (var i = 0; i < length; i ++ ) {
        index++;
        data[i] = '内容' + parseInt(index);
    }
    res.send(data);
})

实现一个天气预报页面

相关文章

网友评论

      本文标题:进阶任务十五-jQuery动画与Ajax

      本文链接:https://www.haomeiwen.com/subject/mrnvdxtx.html