• 使用jQuery操作DOM


    一.jQuery操作样式
            /* 单个样式 */
            $("li:first").css("color","#009933");
            /* 多个样式 */
            $("li:eq(1)").css({"color":"#FFFF00","background":"#FFCCCC"});
            /* 添加样式,引用style的.xxxxx */
            $("li:eq(2)").addClass("lis");
            /* 判断有没有样式 */
            if($("li:eq(2)").hasClass("lis")){
                /* 删除样式 */
                $("li:eq(2)").removeClass("lis");
            }
            $("input").click(function(){
                /* 样式切换 */
                $("li:last").toggleClass("chx");
            });
        二.jQuery操作html
            /*获取标签中的html代码*/
            $("ul").html()
            /*指定标签中的html代码*/
            $("ul").html("<li>****</li>");
            
        三.jQuery操作text文本
            /*获取标签中的text代码*/
            $("li:first").text();
            /*指定标签中的text文本*/
            $("li:first").text("****");
            
        四.jQuery操作value属性值
            /*获取标签中的value属性值*/
            $("input").val();
            /*指定标签中的value属性值*/
            $("input").val("****");
            
        五.jQuery拼接元素
            $("ul").append("<li>****</li>");
            
        六.jQuery操作节点
            /* 创建节点 */
            var $element=$("<li style='color:Fuchsia'>**</li>");
            
            /* 父子级后置追加节点 */
            /* $("ul").append($element);
            $element.appendTo($("ul"));
             */
            
            /* 父子级前置追加节点 */
            /* $("ul").prepend($element);
            $element.prependTo($("ul")); */
            
            /* 同辈追加节点 */
            /* $("li:eq(2)").after($element); */
            /* $("li:eq(2)").before($element); */
            /* $element.insertBefore($("li:eq(2)")); */
            
            /* 删除 */
            /* $("li:first").remove(); */
            /* $("li:first").empty(); */
            /* $("li:first").detach(); */
            
            /* 替换 */
            /* $("li:first").replaceWith($element); */
            /* $element.replaceAll($("li:first")); */
            
            
            
            $("li:first").toggle(function(){
                $(this).css("color","green");
            },function(){
                $(this).css("color","red");
            }
            
            );
            /* 克隆 */
            var $wht=$("li:first").clone(false);
            $("ul").append($wht);
        七.获取属性
            /*设置单个属性*/
            $("li:first").attr("style","color:red");
            /*设置多个属性*/
            $("li:first").attr({"style":"color:red","id":"wht2"});
            /*设置删除属性*/
            $("li:first").removeAttr("style");
        八.遍历节点
            /* 子元素 */
            var lis=$("ul").children("li:eq(2)");
            
            /*同辈元素*/
            alert($("li:first").next().text());
            alert($("li:eq(3)").prev().text());
            alert($("li:eq(3)").prevAll().text())
            
            $("li:eq(2)").siblings().css("color","blue");
            /*祖先元素*/
            $("li:first").parents().css("background","red")
            /*父级元素*/
            $("li:first").parent().css("background","yellow");

  • 相关阅读:
    Apache配置虚拟主机的三种方法(基于IP、端口、域名)
    Apache httpd.conf配置详解
    php-fpm配置文件详解
    php-fpm 三种运行模式
    Nginx优化详解(超详细)
    nginx反向代理实现获取用户真实ip
    WordCount示例深度学习MapReduce过程(1)
    Hadoop学习笔记:MapReduce框架详解
    Locality Sensitive Hash 局部敏感哈希
    Hash表算法
  • 原文地址:https://www.cnblogs.com/chx9832/p/11050581.html
Copyright © 2020-2023  润新知