zoukankan      html  css  js  c++  java
  • 常用的Jquery对元素进行操作或获取设置属性的写法

    最近经常用到前台代码问题,发现一些关于Jquery的知识点有些遗忘掉,故作笔记以记之

    1.获取元素(不同的获取方式)

    <html>
    <head>
    <script type="text/javascript" src="/jquery/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $(".btn1").click(function(){
        $("p").width(200);
        $("#contentDiv").width($("p").width()+200)
        $("#tip1").text($("p").width());
        $("#tip2").text($("div").width());
      });
    });
    </script>
    </head>
    <body>
    <p style="background-color:yellow">This is a paragraph.</p>
    <button class="btn1">改变宽度</button>
    <p>.....................</p>
    <div id="contentDiv" style="background-color:#6868da">
    <span>{------test----------}</span>
    </div>
    <p>.....................</p>
    <p>本段落的宽度分别是 <span id="tip1"> ? </span> 和 <span id="tip2"> ? </span> px。</p>
    </body>
    </html>

    ①元素的特性获取:如:<p>   

    $("p").width(200);

    ②根据元素的id或class获取:如:<span id="tip1"> ,<p style="background-color:yellow">

    $("p").width(200);
    $(".btn1").click();
    $("#contentDiv").width($("p").width()+200)    注意前面的 .  和  # 用于区分

    2.对元素进行属性设置或进行操作

      主要是通过把需要设置的参数或者函数写在方法内的括号里,如:

       $("p").width(200);
       $(".btn1").click(function()...);
       $("#tip1").text($("p").width());


  • 相关阅读:
    自动关联
    如何提高测试效率
    检查点
    windows server 2008 安装vs2008 的问题
    【转】xampp mysql 忘记密码的解决方案
    zencart 目录产品显示控制
    静态html文件执行php语句的方法
    UNIX主机访问PHP程序提示“Internal Server Error”的处理办法
    【转】javascript 点击 <a> 链接
    u880刷机
  • 原文地址:https://www.cnblogs.com/JesseP/p/11278563.html
Copyright © 2011-2022 走看看