在JavaScript中使用反引号(backtick)将导致“无法读取null的属性'style'

时间:2019-05-14 16:24:51

标签: javascript html css pug

我正在尝试使用JavaScript在动态生成的html id上生成click事件。

我使用了反引号来注册click事件,但是我得到了 can not read property 'style' of null

如果您检查呈现的HTML,通常在css id标识符之后输出${post_.id},但是无法读取事件侦听器注册的样式。

我犯了什么错误?

index.pug

...
head
      script.
      function click(id) => {
        let element = document.getElementById(id);
        if element.style.display == 'none' {
          element.style.display = 'block'
        }
        else {
          element.style.display = 'none'
        }
      }
...
body
...
      //- pug templates
      each post in posts

      //- not working
      a(onclick=`click(div_${post._id})`) update

      div(id=`div_${post._id}` style="display: none;)

0 个答案:

没有答案