Ways to call Jquery function methods

1

What are the proper ways to call features It is mandatory to call document ??, to make the calls

$(document).ready(function(){
    $("a").click(function(){
        alert("The paragraph was clicked.");
});
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="javascript:abrir_cerrar_reg();"><h5>Registro</h5></a>
    
asked by Gamez 26.03.2017 в 15:18
source

1 answer

2

The .ready method gives you a secure way to execute JavaScript code as soon as the secure Manipular DOM (Document Object Model) page, otherwise it could not be handled securely (Your Script could be referring to the code of your page that is not yet "loaded") until the document is "ready".

Now, the way JQUERY detects this state of readiness for the developer is through $ (document) .ready () , making use of your same example:

<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("a").click(function() {
        alert("The paragraph was clicked.");
      });
    });
  </script>
</head>

<body>
  <a href="javascript:abrir_cerrar_reg();">
    <h5>Registro</h5>
  </a>
</body>

</html>

Another way to make the call of the event, very common among experienced developers is to use the $ () typography for $ (document) .ready () . If you want to write code so that people who do not have experience can understand, it is better to use the long form, for your own serious example:

<html lang="en">

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script type="text/javascript">
    $(function() {
      $('.llamada').click(function() {
        alert("The paragraph was clicked.");
      });
    });
  </script>
</head>

<body>
  <a href="" class="llamada">
    <h5>Registro</h5>
  </a>
  <!--Puedes llamar al evento instanciando la clase-->
</body>

</html>

This way you would not be forced to write $ (document) .ready () . You can also pass a function in a call to $ (document) .ready () instead of going to an anonymous function.

// Pasando un nombre de función en ves de una función anónima.

function readyFn( jQuery ) {
    // Codigo que se ejecutara cuando el documento este listo
}

$( document ).ready( readyFn );
// o:
$( window ).on( "load", readyFn ); //También puedes hacer uso de esta manera.

Read the documentation Here . Greetings

    
answered by 26.03.2017 / 17:27
source