[英]Execute javascript on html partial render in Ruby on Rails
I am trying to get some elements of a partial whenever it is rendered.我试图在渲染时获取部分的一些元素。 The problem is that I can use
document.addEventListener('DOMContentLoaded', () => {});
问题是我可以使用
document.addEventListener('DOMContentLoaded', () => {});
when the whole page is rendered but I am not able to figure out how to get the elements everytime the html partial is rendered.当整个页面被呈现但我无法弄清楚每次呈现 html 部分时如何获取元素。
js code js代码
window.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('answer_choices');
});
How can I call this event whenever the partial is rendered.每当呈现部分时如何调用此事件。 I can include the javascript file in the partial itself but it creates a lot of overhead.
我可以在部分本身中包含 javascript 文件,但它会产生很多开销。 So this javascript file is included in the main html file but I want the
querySelectorAll
function to be triggered whenever the html partial is rendered.所以这个 javascript 文件包含在主 html 文件中,但我希望在呈现 html 部分时触发
querySelectorAll
函数。 How can I do the same?我怎样才能做同样的事情?
If you use Jquery - you can do it in that way:如果您使用 Jquery - 您可以这样做:
$(function() {
document.querySelectorAll('answer_choices');
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.