[CoffeeScript] Level 2: jQuery to CoffeeScript -- Ex

JS to Coffee I

Convert the commented jQuery code below to CoffeeScript.

复制代码
# jQuery(function($) {
#   $('#newCoffee a').click(function() {
#     alert('New coffee added');
#   });
# });


jQuery ($) ->
  $("#newCoffee a").click ->
    alert('New coffee added')
复制代码

 

JS to CS - Part II

Convert the commented jQuery code below to CoffeeScript and use CoffeeScript-style string interpolation.

# $('#newCoffee a').click(function() {
#   var name = prompt('Name of coffee:');
#   alert("New coffee added: " + name);
# });

$("#newCoffee a").click ->
  name = prompt('Name of coffee:')
  alert("New coffee added: #{name}")

 

JS to CS - Part III

Convert the commented jQuery to CoffeeScript and use CoffeeScript-style string interpolation.

复制代码
# $('#newCoffee a').click(function() {
#   var coffee, name;
#   name = prompt('Name of coffee:');
#   coffee = $("<li>" + name + "</li>");
#   $('ul.drink').append(coffee);
# });

$("#newCoffee a").click ->
  name = prompt('Name of coffee')
  coffee = $("<li> #{name} </li>")
  $('ul.drink').append(coffee);
复制代码

 

JS to CS - Part IV

Convert the commented jQuery code below to CoffeeScript. Use @ instead of this.

# $('.drink li a').click(function(e) {
#   e.preventDefault();
#   alert($(this).text());
# });

$('.drink li a').click (e) ->
  e.preventDefault();
  alert($(@).text());

 

JS to CS - Part V

Convert the commented jQuery code below to CoffeeScript

复制代码
# $('.drink li').mouseenter(function() {
#   $(this).find('span').show();
# });
# $('.drink li').mouseleave(function() {
#   $(this).find('span').hide();
# });

$('.drink li').mouseenter ->
  $(@).find('span').show()
  
$('.drink li').mouseleave ->
  $(@).find('span').hide()
复制代码

 

JS to CS - Part VI

Convert the commented jQuery code below to CoffeeScript.

//The hover function takes two functions as its arguments, like so:

$('selector').hover(
  ->
    $(@).find('span').show();
  ->
    $(@).find('span').hide();
 )
复制代码
# $('.drink li').hover(function() {
#   $(this).find('span').show();
# }, function() {
#   $(this).find('span').hide();
# });

$('.drink li').hover(
  ->
     $(@).find('span').show();
  ->
     $(@).find('span').hide();
  )
复制代码

 

posted @   Zhentiw  阅读(452)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示