【发布时间】:2021-10-07 21:55:34
【问题描述】:
我有一个内部带有 javascript 的 html,它使用 rails 部分。我无法从 rails partial 获取元素,而如果我在不使用 rails partial 的情况下放置按钮,我就能让它工作。
<%= render 'common_component_a', text: 'hello world', elmid: 'grab-this'%>
<script>
if (document.getElementById('grab-this')) {
document.getElementById('grab-this').onclick = function() {
...
};
}
</script>
在我的common_component_a 中,我有一个包含一些 css 样式的部分
<section>
<span> Part A </span>
<button id=<%= elmid %>><%= text %></button>
</section>
【问题讨论】:
-
您真的应该考虑使用类而不是 id 并将此代码放在资产管道中的 JS 文件中。这看起来很像一个自己造成的问题。