JavaScript DHTML/jQuery/Form Button

Материал из Web эксперт
Версия от 07:23, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add action to button with ID

 
<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>
        
          <div>
            <span>A</span>
            <span>B</span> 
            <span>C</span> 
            <span>D</span> 
            <span>E</span> 
            <span>F</span> 
            <span>G</span> 
            <span>H</span>
          </div>
    </body>
</html>



Bind click action to button

   
<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" />

    <div style="background:#eee;" class="contentToChange">
        <h2>Header 2</h2>
       
        <p class="firstparagraph">Lorem ipsum <em>dolor</em> sit amet, consectetuer <em>adipiscing</em> elit, sed diam nonummy nibh euismod <em>tincidunt</em> ut laoreet dolore magna aliquam erat <strong>volutpat</strong>. Ut wisi enim ad minim <em>veniam</em>, quis nostrud exerci <strong>tation</strong> ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
        
        <p class="secondparagraph">Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse <strong>molestie</strong> consequat, vel illum <strong>dolore</strong> 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 <strong>adipiscing</strong> elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
        
        <p class="thirdparagraph">Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea <em>commodo</em> consequat. Duis autem vel eum iriure dolor in hendrerit in <em>vulputate</em> 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 <strong>feugait</strong> nulla facilisi.</p>
        
        <p class="fourthparagraph">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, <strong>quis</strong> nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
        
        <p class="fifthparagraph">Lorem ipsum <em>dolor</em> sit amet, consectetuer <em>adipiscing</em> elit, sed diam nonummy nibh euismod <em>tincidunt</em> ut laoreet dolore magna aliquam erat <strong>volutpat</strong>. Ut wisi enim ad minim <em>veniam</em>, quis nostrud exerci <strong>tation</strong> ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
        
        <p class="sixthparagraph">Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse <strong>molestie</strong> consequat, vel illum <strong>dolore</strong> 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 <strong>adipiscing</strong> elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
        
    </div>
  </body>
</html>



Can bind and unbind events to the button.

 

<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>



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

 

<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>



Disables buttons greater than the 1st button.

 
<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>



Get disabled button

    
<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>
       <div><button disabled="disabled">First</button> - <span></span></div>
  </body>
</html>



Get form button

    
<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>
  <div>
  </div>
  </body>
</html>



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

 
<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("<div>" + $(this).text() + "</div>");
                });
                
         
        });
    </script>

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



Use index to get the clicked button index

 

<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>
          <div>A div</div>
          <button>0</button>
          <button>1</button>
          <button>2</button>
          <button>3</button>
          <span></span>
    </body>
</html>