[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(); )
分类:
CoffeeScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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工具