zoukankan      html  css  js  c++  java
  • 用jquery获取当前点击对象

      本来以为用$(this)可以很容易获得当前点击的对象,可在用的时候,发现并没有想象的那么简单。$(this)获取的是[object Object],并不能用jquery的方法去寻找当前对象的前后节点。代码如下:

            

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
    function launch(){
    var a=$().parent().prev().children(":eq(0)").val();
    alert(a);
    }
    </script>
    </head>

    <body>
    <table>
    <tr>
    <td><input type="text" value="还是这里"/></td>
    <td></td>
    </tr>
    <tr>
    <td><input type="text" value="这里"/></td>
    <td><a onclick="launch()">点击</a></td>
    </tr>
    </table>
    </body>
    </html>

    代码执行的效果是:

            

    将代码做如下改动:

          

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
    function launch(a,b){
    var a=$(a).parent().prev().children(":eq(0)").val();
    alert(a);
    }
    </script>
    </head>

    <body>
    <table>
    <tr>
    <td><input type="text" value="还是这里"/></td>
    <td></td>
    </tr>
    <tr>
    <td><input type="text" value="这里"/></td>
    <td><a onclick="launch(this,event)">点击</a></td>
    </tr>
    </table>
    </body>
    </html>

    则代码的执行效果:

            

    就可以获取当前点击的对象。

      该方法就可以非常方便的获取点击对象和点击对象相关的节点。

  • 相关阅读:
    5.6 Go 常用函数
    5.5 Go defer
    5.4 Go 闭包
    5.3 Go 匿名函数
    5.2 Go 包与函数
    python 通过pytz模块进行时区的转换,获取指定时区的时间
    前端在js中获取用户所在地区的时间与时区
    Python2 指定文件编码格式需要注意的地方
    linux 使用进程管理工具 supervisor
    Python 私有变量中两个下划线 _ _item 与 一个下划线的区别 _item
  • 原文地址:https://www.cnblogs.com/ncwuwsh/p/2948783.html
Copyright © 2011-2022 走看看