我正在使用 iframe 在我的 Rails 应用程序中显示托管在同一域中的一些静态 html 文件。我向 iframe 内的元素添加一个带有远程属性的链接,希望能够进行就地 ajax 调用来创建新的嵌套资源,如下所示:
----project.js----
var f = $( ".myFrame" );
f.load(function(){
$(this).find('.jd-details-title').append('<a data-remote="true" href="/projects/10/issues/new" id="new_link">New Issue</a>');
});
----javadocs.html.erb----
<iframe width="100%"
height="1200"
frameborder="0"
scrolling="auto"
class='myFrame'
src= <%= /path/to/static/htmls/ %> >
</iframe>
当我在iframe内点击这个链接时,iframe会被重定向到新一期的html页面,但是当它移到iframe之外时,它会神奇地调用新一期的js版本。
我在这里缺少什么?如何修改此代码以获得相同的行为?
最佳答案
remote: true
由 Rails 上名为 jquery_ujs
的 javascript 库控制(您可以在 application.js 中看到它)。由于您的 iframe 页面不包含此库,因此它将无法理解 remote: true
(请记住,iframe 内部的页面与父页面完全隔离)。
在这种情况下,我认为您必须手动处理 iframe 中的链接。例如:
$("link_id_inside_iframe").click(function(e){
e.preventDefault();
var link = $(this).attr("href");
$.getScript(link);
});
关于javascript - rails : a simple remote: true call does not generate js response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24225688/