jQuery_02

简介: jQuery_02

1.工具下的类方法使用

1.1js与jQuery转换

<head>
<script src="js/jquery-3.6.4.js"></script>
</head>
<script>
    $(function(){
      $("#obtn").click(function(){
                //jquery转化为原生态js对象处理
                //根据数组下标获取
        //var val=$("#oInput")[0].value;
                //根据get方法来转换
                var val=$("#oInput").get(0).value;
        console.log(val);
       })
       })
        $(function(){
      $("#obtn").click(function(){
               //原生态js对象转换成jQuery对象处理
         var oInput=document.getElementById("oInput");
               //将一个原生态js对象传入$()内直接可以转换
         console.log($("oInput").val());
      })
       })
       })
</script>
    <input type="text" id="oInput">
    <button id="obtn">点击</button>

1.2.$对象中的类方法

1.2.1.each()方法       遍历元素的方法

       

<script>
    $(function(){
      $("#obtn").click(function(){
            //使用方法:直接调用或通过$()返回的对象调用
            //使用$.each(obj,function(){})方法的形式来遍历输出  对象   对象数组
            //数组的定义
       var arr=["aa","bb","cc","dd"];
       $.each(arr,function(a,b){
           console.log(a,b,arr[a]);
       })
       })
    $(function(){
      $("#obtn").click(function(){
      //对象的定义
       var stu={
        "name":"李永安",
        "sex":"男",
        "age":18
       };
       })
    $(function(){
      $("#obtn").click(function(){
            //a代表属性名称   b代表属性值
       $.each(stu,function(a,b){
        console.log(a,b);
       });
       })
    $(function(){
      $("#obtn").click(function(){
            //对象数组
       var stus=[
        {
          "name":"李永安",
            "sex":"男",
            "age":18
        },
        {
              "name":"aa",
          "sex":"男",
          "age":18
        },
        {
          "name":"bb",
          "sex":"男",
          "age":18
        }
       ];
       })
    $(function(){
      $("#obtn").click(function(){
       $.each(stu,function(index,obj){
            // conloge.log(index,obj)
                  //name  属性名称   value  属性值
         $.each(obj,function(name,vlue){})
                 console.log(index,name,value,obj[name])  
         });
       })
</script>

1.2.2.type()方法  获取属性类型

console.log($.type(123));
nsole.log($.type(new Date()));
console.log($.isFunction(method));
console.log($.isArray(arr));

1.2.3.trim()方法   去除左右空格

​var str="    a  bc   ";
console.log(str.length);
console.log($.trim(str).length);

1.2.4.paeseJSON   用户后台查到的数据传输给客户端,客户拿到数据进行解析,是否满足JSON格式;AJAS  无刷新技术

    $(function(){
      $("#obtn").click(function(){
             var mystul={"name":"张三"};
       console.log($.type(mystul));
       var mystu2="{\"name\":\"张三\"}";
             console.log($.type(mystul2));
             //作用:将一个满足JSON字符串的数据转成对应的对象或者对象数组
       var newstu=$.parseJSON(mystu2);
       console.log($.type(newstu))
             })

2.css属性和样式设置

2.1.attr():获取某个标签属性的值,或设置某个标签属性的值

​ <head>
      <script src="js/jquery-3.6.4.js"></script>
    <style>
    .abc{
      border: 10px solid red;
    </style>
  </head>
     <body>  
         $("#btn1").click(function(){
         console.log($("img").arrt("src"));
         $("img").attr("src","img/羽绒服.jpg");
     <script>
        <img src="img/登山鞋.jpg" alt="" title="服装">
    <button id="btn1">btn1</button>
      </script>
      </body>

2.2.removeAttr():删除某个标签属性

​ <head>
      <script src="js/jquery-3.6.4.js"></script>
    <style>
    .abc{
      border: 10px solid red;
    </style>
  </head>
     <body>  
         $("#btn1").click(function(){
         console.log($("img").arrt("src"));
         $("img").removeAttr("title");
     <script>
        <img src="img/登山鞋.jpg" alt="" title="服装">
    <button id="btn1">btn1</button>
      </script>
      </body>

2.3.addClass():给某个标签添加class属性值

​ <head>
      <script src="js/jquery-3.6.4.js"></script>
    <style>
    .abc{
      border: 10px solid red;
    </style>
  </head>
        $("#btn1").click(function(){
               $("img").addClass("abc");
        })
        <img src="img/登山鞋.jpg" alt="" title="服装">
    <button id="btn1">btn1</button>

2.4.removeClass():删除某个标签class属性值

  <head>
      <script src="js/jquery-3.6.4.js"></script>
    <style>
    .abc{
      border: 10px solid red;
    </style>
  </head>
    <script>
    $(function(){
      $("#obtn").click(function(){
            $("img").removeClass("abc");
            })
    </script>
    <input type="text" id="oInput">
    <button id="obtn">点击</button>

2.5.prop():和attr()类似,区别在于prop用于属性值为Boolean类型的情况

  <head>
      <script src="js/jquery-3.6.4.js"></script>
    <style>
    .abc{
      border: 10px solid red;
    </style>
  </head>
    <script>
    $(function(){
      $("#obtn").click(function(){
                $("img").prop("src","img/羽绒服.jpg");
        $("input:checkbox").prop("checked",true);
        $("input:checkbox").attr("checked",true);
            })
    </script>
    <input type="text" id="oInput">
    <button id="obtn">点击</button>

2.6.html():获取某一个标签体内容(注意:该标签体中可以包含子标签)

  <head>
      <script src="js/jquery-3.6.4.js"></script>
    <style>
    .abc{
      border: 10px solid red;
    </style>
  </head>
    <script>
    $(function(){
      $("#obtn").click(function(){
                console.log($("odiv").html());
            })
    </script>
    <input type="text" id="oInput">
    <button id="obtn">点击</button>
        <div id="odiv" style="width: 100%; height:100px; border: 1px solid red;"></div>

2.7.text():获取某一个标签体内容(注意:该标签体不能包含子标签)

    <head>
      <script src="js/jquery-3.6.4.js"></script>
    <style>
    .abc{
      border: 10px solid red;
    </style>
  </head>
    <script>
    $(function(){
      $("#obtn").click(function(){
                console.log($("odiv").text());  
            })
    </script>
    <input type="text" id="oInput">
    <button id="obtn">点击</button>
        <div id="odiv" style="width: 100%; height:100px; border: 1px solid red;"></div>

2.8.val():主要用户获取/设置输入框的值

  <head>
      <script src="js/jquery-3.6.4.js"></script>
    <style>
    .abc{
      border: 10px solid red;
    </style>
  </head>
    <script>
    $(function(){
      $("#obtn").click(function(){
               // console.log($("odiv").val());
               // 获取值
                  console.log($("#username").val());
               // 设置值
          $("#username").val("你好");
            })
    </script>
    <input type="text" id="oInput">
    <button id="obtn">点击</button>
        <div id="odiv" style="width: 100%; height:100px; border: 1px solid red;"></div>
​        <input type="text" id="username" value="1232432">

3.样式设置

  <head>
      <script src="js/jquery-3.6.4.js"></script>
    <style>
    .abc{
      border: 10px solid red;
    </style>
  </head>
  <body>
    <script>
    $(function(){
      $("#btn1").click(function(){
         //console.log($("img").css("width"));  
                 //设置一个属性
         $("img").css("width","50px");
                 //设置多个属性
           $("img").css({"width":"50px","height":"300px"});
       }) 
  <img src="img/登山鞋.jpg" alt="" title="服装">
  <button id="btn1">btn1</button>
目录
相关文章
|
关系型数据库 测试技术 数据库
使用Docker搭建测试用例管理平台TestLink:简易指南
使用Docker搭建TestLink测试管理软件的步骤如下:首先,拉取`bitnami/mariadb`和`bitnami/testlink-archived`镜像。然后,启动MariaDB容器,创建数据库。接着,启动TestLink容器并连接到MariaDB。检查容器状态确保它们已启动。最后,访问`localhost:8099`以使用TestLink,默认用户名为`user`,密码为`bitnami`。这样,你就能在本地便捷地进行测试管理了。
|
设计模式 存储 缓存
Java中的抽象类、接口、设计模式、包装类和泛型(附带相关面试题)
一.抽象类(abstract),二.接口(interface),三.设计模式,四.包装类,五.泛型
626 0
|
Oracle 关系型数据库 数据库
关系型数据库Oracle增量备份
【7月更文挑战第18天】
211 2
|
边缘计算 人工智能 测试技术
什么是虚拟机技术?
拟机技术作为现代计算环境中的重要组成部分,极大地丰富了我们对资源管理和系统部署的理解与实践。本文将深入探讨虚拟机的定义、工作原理、应用场景、优势、主要技术以及未来发展趋势,帮助读者全方位地理解虚拟机这一强大技术。
609 7
|
机器学习/深度学习 算法 大数据
【机器学习】朴素贝叶斯算法及其应用探索
在机器学习的广阔领域中,朴素贝叶斯分类器以其实现简单、计算高效和解释性强等特点,成为了一颗璀璨的明星。尽管名字中带有“朴素”二字,它在文本分类、垃圾邮件过滤、情感分析等多个领域展现出了不凡的效果。本文将深入浅出地介绍朴素贝叶斯的基本原理、数学推导、优缺点以及实际应用案例,旨在为读者构建一个全面而深刻的理解框架。
465 1
|
机器学习/深度学习 算法 数据挖掘
CocosCreator3.8研究笔记(二十一)CocosCreator Tween系统理解(2)
CocosCreator3.8研究笔记(二十一)CocosCreator Tween系统理解
266 0
|
监控 数据可视化 安全
Baumer工业相机堡盟相机如何使用Trace功能(相机日志追踪的使用和优点以及行业应用)(C++)
Baumer工业相机堡盟相机如何使用Trace功能(相机日志追踪的使用和优点以及行业应用)(C++)
230 0
|
SQL 运维 Oracle
【大数据开发运维解决方案】ogg(GoldenGate)三大进程常用参数
PORT 7809 管理进程的监听端口,默认使7809,当7809不可用时会从DYNAMICPORTLIST定义的列表中选择一个可用的端口,主要用于本地goldengate进程之间的通信 DYNAMICPORTLIST 7810-7860 动态端口,可以指定最大256个可用端口列表,用于主端和备端的进程通信,当目标端有防火墙设置时或者主端的投递进程传送数据要经过防火墙(就是主端有防火墙设置时)才能到达备端时,需要在网络上开通指定的端口。源端和目标段的Collector、Replicat、GGSCI进程通信也会使用这些端口,指定足够的端口去容纳进程数的扩张,这样就不需要停止和重启管理器进程
【大数据开发运维解决方案】ogg(GoldenGate)三大进程常用参数
|
Go 索引
Go panic & recover 使用注意点:
Go panic & recover 使用注意点:
535 0
Go panic & recover 使用注意点: