JavaScript DHTML/jQuery/Form Button

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

Add action to button with ID

   <source lang="html4strict">

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
               $("#hideHandler").click(function () {
                     $("span:first-child").hide("fast", function () {
                       $(this).prev().hide("show", arguments.callee); 
                     });
               });
               $("#showHandler").click(function () {
                     $("span").show(6000);
               });
       });
   </script>
 </head>
 <body>
   <body>
         <button id="hideHandler">Hide</button>
         <button id="showHandler">Show</button>
       
           A
           B 
           C 
           D 
           E 
           F 
           G 
           H
   </body>

</html>

 </source>
   
  


Bind click action to button

   <source lang="html4strict">
  

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <style type="text/css">
       a.test { font-weight: bold; color:red;}
   </style>
   <script type="text/javascript">
$(document).ready(function(){
   $("input.buttonA").click(function(){ $("div.contentToChange").find("p.firstparagraph:hidden").slideDown("slow"); });
   $("input.buttonB").click(function(){ $("div.contentToChange").find("p.firstparagraph:visible").slideUp("slow"); });
});
   </script>
 </head>
 <body>
   <input type="button" value="Slide In" class="buttonA" />
   <input type="button" value="Slide Out" class="buttonB" />

Header 2

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. <p class="fifthparagraph">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

 </body>

</html>


 </source>
   
  


Can bind and unbind events to the button.

   <source lang="html4strict">

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
               
           function aClick() {
             alert("action");
           }
           $("#bind").click(function () {
             $("#myButton").click(aClick).text("Binded");
           });
           $("#unbind").click(function () {
             $("#myButton").unbind("click", aClick).text("Not Binded");
           });
       });
   </script>
 </head>
 <body>
   <body>
         <button id="myButton">Not Binded</button>
         <button id="bind">Bind Click</button>
         <button id="unbind">Unbind Click</button>
      
                        
   </body>

</html>

 </source>
   
  


Create a button that"s able to clone itself - and have the clones themselves be clonable.

   <source lang="html4strict">

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
            $("button").click(function(){
                $(this).clone(true).insertAfter(this);
            });
       });
   </script>
 </head>
 <body>
   <body>
           <button>Clone Me!</button>
                
   </body>

</html>

 </source>
   
  


Disables buttons greater than the 1st button.

   <source lang="html4strict">

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
           $("button:gt(1)").attr("disabled","disabled");
     
       });
   </script>
 </head>
 <body>
   <body>
     <button>0th Button</button>
     <button>1st Button</button>
     <button>2nd Button</button>
   </body>

</html>

 </source>
   
  


Get disabled button

   <source lang="html4strict">
   

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
            $("button[disabled]").next().text("data");
       });
   </script>
   <style>
     .y { background:yellow; }
 </style>
 </head>
 <body>
   <body>
<button disabled="disabled">First</button> -
 </body>

</html>



 </source>
   
  


Get form button

   <source lang="html4strict">
   

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
           var input = $(":button");
           $("div").text(input.length);
       });
       
           
       
   </script>
 </head>
 <body>
   <form>
   
   <input type="button" />
   
 </form>
 </body>

</html>



 </source>
   
  


On click, replace the button with a div containing the same word.

   <source lang="html4strict">

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
             $("button").click(function () {
$(this).replaceWith("
" + $(this).text() + "
");
               });
               
        
       });
   </script>


 </head>
 <body>
   <body>
         <button>First</button>
                
   </body>

</html>

 </source>
   
  


Use index to get the clicked button index

   <source lang="html4strict">

<html>

 <head>
   <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
   <script type="text/javascript">
       $(document).ready(function(){
           $("button").click(function(e) {
             switch ($("button").index(this)) {
               case 0 :
                 $("div").text("0");
                 break;
               case 1 :
                 $("div").text("1");
                 break;
               case 2 :
                 $("div").text("2");
                 break;
               case 3 :
                 $("div").text("3");
                 break;
             }
       
             $("span").text("" + value);
           });
       });
   </script>
 </head>
 <body>
   <body>
A div
         <button>0</button>
         <button>1</button>
         <button>2</button>
         <button>3</button>
         
   </body>

</html>

 </source>