Ruby on rails 4.0 + javascript not loading

前端 未结 3 507
故里飘歌
故里飘歌 2021-01-07 13:17

I have a javascript file called \"front.js\" i load from \"application.js\" like usual with this code:

//= require front

Inside \"front.js\

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 13:35

    There are three ways by which you can do it.

    a. You can wrap your jquery code inside a function and then call that function on page load. If your function is ready then you can do:

    $(document).ready(ready);
    $(document).on('page:load', ready);
    

    b. You can use jquery's live event or "on" event:

    $(document).on("click","#test",function(){
     // your code
    });
    

    c. You can use the jquery-turbolinks gem which will bind the Rails Turbolink events to the document.ready events so you can write your jQuery in the usual way. For more information refer to here

提交回复
热议问题