JavaScript DHTML/jQuery/closest

Материал из Web эксперт
Перейти к: навигация, поиск

Add click event to document

  

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
          $(document).bind("click", function (e) {
               $(e.target).closest("span").text("asdf");
          });
        });
    </script>
  </head>
  <body>
    <body>
         <div><span>span</span><span>span</span><span>span</span></div>
  </body>
</html>



Get closest tag

   
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
          $(document).bind("click", function (e) {
               $(e.target).closest("span").text("asdf");
          });
        });
    </script>
  </head>
  <body>
    <body>
         <div><span>span</span><span>span</span><span>span</span></div>
  </body>
</html>