coffeescript没有执行代码

coffeescript is not executing code

本文关键字:代码 执行 coffeescript      更新时间:2023-09-26

我正在尝试让我的点击函数更改CSS代码,然后执行一个函数。这是我目前拥有的代码:

ready: ->
   $("#titleDD").click ->
     $("#titleDD").css('text-decoration', 'underline');
     $("#catDD").css('text-decoration', 'none');    
   $("#catDD").click ->
     $("#catDD").css('text-decoration', 'underline');
     $("#titleDD").css('text-decoration', 'none'); 
     `console.log("hello2");`
     `console.log("hello");`

当我运行代码时,一旦我的网页开始运行,控制台就会立即显示hello2和hello。当我点击#catDD hello和hello2时,标题DD文本装饰不会改变。有人能解释为什么所有的代码都没有执行吗?感谢

尝试:

ready: ->
   $("#titleDD").click ->
     $("#titleDD").css('text-decoration', 'underline');
     $("#catDD").css('text-decoration', 'none');   
     return 
   $("#catDD").click ->
     $("#catDD").css('text-decoration', 'underline');
     $("#titleDD").css('text-decoration', 'none'); 
     console.log "hello2"
     console.log "hello"
     return
   return

生成:

  ready: function() {
    $("#titleDD").click(function() {
      $("#titleDD").css('text-decoration', 'underline');
      $("#catDD").css('text-decoration', 'none');
    });
    $("#catDD").click(function() {
      $("#catDD").css('text-decoration', 'underline');
      $("#titleDD").css('text-decoration', 'none');
      console.log("hello2");
      console.log("hello");
    });
  }

(很抱歉,我对我在coffeescript中的返回进行了分析——如果你不小心,它们会生成额外的js,没有人想在函数末尾看到return console.log("hello");