zoukankan      html  css  js  c++  java
  • Python学习第87天(jQuery属性操作、循环方法)

    一、属性操作

      1.属性

        $(" ").attr( );      主要用于获取用户自主设定的属性数据,如果设定两个参数,则是将原属性参数修改为第二个属性参数。

        $(" ").removeAttr( );

        $(" ").prop( );         主要用于获取固有属性数据,参数形式和attr一样。

        $(" ").removeProp( );
      2.CSS类

        $(" ").addClass(class|fn);    增加节点类名

        $(" ").removeClass([class|fn]);  删除节点类名

      3.HTML代码/文本/值

        $(" ").html([val|fn]);      等同于JavaScript中的innerHTML

        $(" ").text([val|fn]);      等同于JavaScript中的innerTEXT

        $(" ").val([val|fn|arr]);     获取固有参数属性的value值

      4.属性设置

        $(" ").css("color","red");    设定节点属性,设置多个属性的时候

        $(" ").css({"color":"red" ,"backgroundcolor” :"red" ’});

    <input id="chk1" type="checkbox" />是否可见
    <input id="chk2" type="checkbox" checked="checked" />是否可见
    <script>
    
    //对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
    //对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
    //像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此
    //需要使用prop方法去操作才能获得正确的结果。
    
    
    //    $("#chk1").attr("checked")
    //    undefined
    //    $("#chk1").prop("checked")
    //    false
    
    //  ---------手动选中的时候attr()获得到没有意义的undefined-----------
    //    $("#chk1").attr("checked")
    //    undefined
    //    $("#chk1").prop("checked")
    //    true
    
        console.log($("#chk1").prop("checked"));//false
        console.log($("#chk2").prop("checked"));//true
        console.log($("#chk1").attr("checked"));//undefined
        console.log($("#chk2").attr("checked"));//checked
    </script>
    
    attr和prop

      二、循环方式

    jquery循环的两种方式
                     方式一
                     li=[10,20,30,40]
                     dic={name:"yuan",sex:"male"}
                     $.each(li,function(i,x){
                         console.log(i,x)
                     })
    
                     方式二
                     $("tr").each(function(){
                         console.log($(this).html())
                      })

      现在用jQuery的方式写一下正反选的问题

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="jquery-1.11.3.min.js"></script>
        <script>
    
                 function selectall(){
    
                     $("table :checkbox").prop("checked",true)
                 }
                 function cancel(){
    
                     $("table :checkbox").prop("checked",false)
                 }
    
                 function reverse(){
    
        </script>
    </head>
    <body>
    
         <button onclick="selectall();">全选</button>
         <button onclick="cancel();">取消</button>
         <button onclick="reverse();">反选</button>
    
         <table border="1">
             <tr>
                 <td><input type="checkbox"></td>
                 <td>111</td>
             </tr>
             <tr>
                 <td><input type="checkbox"></td>
                 <td>222</td>
             </tr>
             <tr>
                 <td><input type="checkbox"></td>
                 <td>333</td>
             </tr>
             <tr>
                 <td><input type="checkbox"></td>
                 <td>444</td>
             </tr>
         </table>
    </body>
    </html>

      以上就是今天所有内容。。。

  • 相关阅读:
    6-MySQL-Ubuntu-操作数据表的基本操作(一)
    5-MySQL-Ubuntu-操作数据库的基本操作语句
    11-Ubuntu-根目录下各目录的功能详细介绍
    4-Ubuntu-启动/关闭/重启mysql服务
    3-Windows-CMD启动mysql服务-连接本地mysql服务-连接远程mysql服务
    2-Ubuntu命令安装mysql服务器和客户端及安装后的简单验证操作
    1-Navicat无法远程连接Ubuntu上的MySQL(已解决)
    10-python基础—索引与切片总结
    Useful Things To Know About Machine Learning (机器学习中一些有用的知识)
    How to Use the TimeDistributed Layer for Long Short-Term Memory Networks in Python 译文
  • 原文地址:https://www.cnblogs.com/xiaoyaotx/p/12953402.html
Copyright © 2011-2022 走看看