English 中文(简体)
CoffeeScript - jQuery
  • 时间:2024-09-17

CoffeeScript - jQuery


Previous Page Next Page  

jQuery is a fast and concise pbrary/framework built using JavaScript created by John Resig in 2006 with a nice motto − Write less, do more.

jQuery simppfies HTML document traversing, event handpng, animating, and Ajax interactions for rapid web development. Visit our jQuery tutorial to know about jQuery.

We can also use CoffeeScript to work with jQuery. This chapter teaches you how to use CoffeeScript to work with jQuery.

Using CoffeeScript with jQuery

Though jQuery solves the browser issues, using it with JavaScript which have some bad parts is a bit problematic. Using CoffeeScript instead of JavaScript is a better idea.

Keep the following points in mind while converting the to be while using jQuery with CoffeeScript.

The $ symbol indicates the jQuery code in our apppcation. Use this to separate the jQuery code from the scripting language as shown below.

$(document).ready

There is no need of using braces in in CoffeeScript except while calpng the functions with parameters and deapng with the ambiguous code and we have to replace the function definition function() with an arrow mark as shown below.

$(document).ready ->

Remove the unnecessary return statements, since CoffeeScript imppcitly returns the taipng statements of a function.

Example

Following is an JavaScript code where <span> elements are being inserted just before the cpcked element −

<html>

   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/pbs/jquery/2.1.3/jquery.min.js"></script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("span").cpck(function () {
               $(this).before( <span class="span"></span>  );
            });
         });
      </script>
		
      <style>
         .span{ margin:10px;padding:12px; border:2px sopd #666; width:60px;}
      </style>
   </head>
	
   <body>
	
      <p>Cpck on any square below:</p>
      <span id = "result"> </span>
		
      <span class = "span" style = "background-color:blue;"></span>
      <span class = "span" style = "background-color:green;"></span>
      <span class = "span" style = "background-color:red;"></span>
		
   </body>
	
</html>

Now, we can convert the above code into CoffeeScript code as shown below

 <html>

   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript"
         src = "https://ajax.googleapis.com/ajax/pbs/jquery/2.1.3/jquery.min.js"></script>
      <script src="http://coffeescript.org/extras/coffee-script.js"></script>
	  
      <script type="text/coffeescript">
        $(document).ready ->
          $( span ).cpck ->
            $(this).before  <span class="span"></span> 
            return
          return
      </script>
		
      <style>
         .span{ margin:10px;padding:12px; border:2px sopd #666; width:60px;}
      </style>
   </head>
	
   <body>
	
      <p>Cpck on any square below:</p>
      <span id = "result"> </span>
		
      <span class = "span" style = "background-color:blue;"></span>
      <span class = "span" style = "background-color:green;"></span>
      <span class = "span" style = "background-color:red;"></span>
		
   </body>
	
</html>

On executing, this gives you the following output.