Button clicks in Jade

大兔子大兔子 提交于 2019-12-05 14:51:55

问题


I'm having difficulty getting a button to execute a javascript function when it is clicked, below is my jade file

extends layout
block content
  - var something = function() {
  -   console.log('something')
  - }

  button(onclick='#{something()}') Click

Where am I going wrong with this?


回答1:


With this line:
button(onclick='#{something()}') Click

you tell Jade that it should paste the content of the function into the value of the onclick attribute.

Just reference the function name:

button(onclick='something()') Click

But this won't work, because the function is only available in the Jade compile step. After this step the generated HTML has no access to the variables which was defined in Jade.

You need to include a JavaScript file or use the script tag:

script.
  var something = function() {
    console.log('something')
  }

button(onclick='something()') Click



回答2:


Just add a space:

button(onclick='#{something()}') Click


来源:https://stackoverflow.com/questions/22715778/button-clicks-in-jade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!