【发布时间】:2011-03-31 13:50:06
【问题描述】:
我有一个包含一些 div 元素的 HAML 页面。单击按钮时,我需要将其注入另一个页面上的 div 中。我该怎么做呢?谢谢
【问题讨论】:
-
您想使用什么技术? javascript和python?
-
ajax/javascript...使用 rails (mvc)
我有一个包含一些 div 元素的 HAML 页面。单击按钮时,我需要将其注入另一个页面上的 div 中。我该怎么做呢?谢谢
【问题讨论】:
您必须从 jQuery.com 添加 jQuery 插件。您可以下载插件或使用链接 http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js作为js文件中的src。
然后使用以下代码
$(document).ready(function(){
$("#your_button_Id").click(function(){
$.ajax({
url: "test.html",
context: document.body,
success: function(response){
$('#div_Id').html(response);
}
});
});
});
希望这会有所帮助!!! 快乐编码。
【讨论】:
如果你想使用 jquery,试试 $('div').load('lol.HTML')
【讨论】:
为简化此过程,请将jQuery library 添加到您的页面。
下面是一个使用jQuery从不同页面加载数据到当前页面的例子:
inject_to = $("div#id"); // the div to load the html into
load_from = "/some/other/page"; // the url to the page to load html from
data = ""; // optional data to send to the other page when loading it
// do an asynchronous GET request
$.get(load_from, data, function(data)
{
// put the received html into the div
inject_to.html(data);
}
请注意,这会针对安全/xss 问题打开,我建议使用 .text 而不是 .html,并且仅从外部页面加载纯文本。更多关于 jQuery ajax 的信息:http://api.jquery.com/category/ajax/
要在单击按钮时执行此操作,请将上述代码放入函数中,如下所示:
function buttonClicked()
{
inject_to = $("div#id"); // the div to load the html into
load_from = "/some/other/page"; // the url to the page to load html from
data = ""; // optional data to send to the other page when loading it
// do an asynchronous GET request
$.get(load_from, data, function(data)
{
// put the received html into the div
inject_to.html(data);
}
}
然后给按钮添加点击事件的事件处理函数,像这样:
$("button#id").click(buttonClicked);
更多关于 jQuery 事件的信息:http://api.jquery.com/category/events/
【讨论】: