jQuery操作DOM

作者: 辉夜乀 | 来源:发表于2017-04-30 16:50 被阅读18次
    1.给元素 $node 添加 class active,给元素 $noed 删除 class active
    $node.addClass("active")
    $node.removeClass("active")
    
    2.展示元素$node, 隐藏元素$node
    $node.show()
    $node.hide()
    
    $node.css("display", "block")
    $node.css("display", "none")
    
    $node.fadeIn()
    $node.fadeOut()
    
    3.获取元素$node 的 属性: id、src、title, 修改以上属性
    $node.attr("id")
    $node.attr("src")
    $node.attr("title")
      //获取属性
    $node.attr("id", "new-id")
    $node.attr("src", "new-src")
    $node.attr("title", "new-title")
      //设置属性
    
    4.给$node 添加自定义属性data-src
    $node.attr("data-src", "xxxx")
    
    5.在$ct 内部最开头添加元素$node
    $ct.prepend($node)
    $node.prependTo($ct)
    
    6.在$ct 内部最末尾添加元素$node
    $ct.append($node)
    $node.appendTo($ct)
    
    7.删除$node
    $node.remove()  //内部数据也删除
    $node.detach()  //内部数据保留
    
    8.把$ct里内容清空
    $ct.empty()
    
    9.在 $ct 里设置 html <div class="btn"></div>
    $ct.html('<div class="btn"></div>')
    
    10.获取、设置$node 的宽度、高度
    $node.width();
    $node.height();
        //content 的宽高,
        //括号内传入参数就是设置
    $node.innerWidth();
    $node.innerHeight();
        //content + padding 的宽高,
        //括号内传入参数就是设置
    $node.outerWidth();
    $node.outerHeight();
        //content + padding + border 的宽高,
        //括号内传入参数就是设置
    $node.outerHeight(true);
    $node.outerWidth(true);
        //content + padding + border + margin 的宽高,
        //不能设置参数
    
    11.获取窗口滚动条垂直滚动距离
    $(window).scrollTop()
    $(document).scrollTop()
        //二者等价
    
    12.获取$node到根节点水平、垂直偏移距离
    $node.offset()
        //获取到根节点水平、垂直偏移距离
    $node.offset({left: 500, top: 300})
        //设置到根节点水平、垂直偏移距离
    
    13.修改$node 的样式,字体颜色设置红色,字体大小设置14px
    $node.css({
        color: "red",
        fontSize: "14px"
    })
    
    14.遍历节点,把每个节点里面的文本内容重复一遍
    $(document).each(function(){
        var $text = $(this).text();
        $(this).text($text+$text)
    })
    
    15.从$ct 里查找 class 为 .item的子元素
    $ct.find(".item")
    
    16.获取$ct 里面的所有孩子
    $ct.children()
    
    17.对于$node,向上找到 class 为'.ct'的父亲,在从该父亲找到'.panel'的孩子
    $node.parents(".ct").find(".panel")
    
    18.获取选择元素的数量
    $node.length
    
    19.获取当前元素在兄弟中的排行
    $node.index()
    

    相关文章

      网友评论

        本文标题:jQuery操作DOM

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