简体   繁体   English

将pug mixin传递给JavaScript

[英]Pass pug mixin to JavaScript

I have a common modal all over my site for showing endnotes. 我在整个网站上都有一个通用的模式来显示尾注。 I wanted to create a quick pug mixin so I don't have to type the structure over and over. 我想创建一个快速的哈巴狗搅拌器,所以我不必一遍又一遍地键入结构。

However, I don't know how to pass the #id into my js toggle_visibility function. 但是,我不知道如何将#id传递到我的js toggle_visibility函数中。 (Note, the js works perfectly, I'm just trying to convert to a mixin) (请注意,js完美运行,我只是尝试转换为mixin)

mixin modal(id, content)
 div.modal(class= id)
  div.modal-content= content
   a.close(onclick="toggle_visibility('= id');") Close 

Here's an example of the pug code working (outside the mixin) 这是哈巴狗代码工作的示例(在mixin之外)

div.modal#downloads-citations
      div.modal-content
        p some content here to show article citations
        a.close(onclick="toggle_visibility('downloads-citations');") Close

What you're looking for is called 'attribute interpolation', and is not directly supported anymore in current versions of Pug (see this page in their docs). 您要查找的内容称为“属性插值”,当前版本的Pug中不再直接支持(请参见其文档中的此页 )。

Fortunately, however, there is an easy workaround, based on the concatenation of strings. 幸运的是,基于字符串的连接,有一个简单的解决方法。 Just replace the last line of your code snippet with this statement: 只需使用以下语句替换代码段的最后一行:

a.close(onclick="toggle_visibility('" + id + "');") Close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM