通过 jQuery.get() 从另一个页面绘制的 html 的阴影盒显示
我对 Ajax 有点陌生,希望大家能建议这两种方法中哪一种最有效。这两种方法似乎都有效,但也许还有更好的方法?
thisPage.html 想要显示从 thatPage.html(在同一主机上)绘制的 html blob,以响应对链接的点击。我使用 Shadowbox 的 html 播放器来显示 blob,并使用 jQuery 的 get() 作为 XMLHttpRequest 的快捷方式。
在我的第一个解决方案中, thisPage.js 包含这个函数:
function loadEntry(entry){
jQuery.get('thatPage.html', function(data){
var elem = document.createElement("div");
elem.innerHTML = "<div class='entry-to-show'>" + jQuery(data).find("a[name='" + entry + "']").parent().parent().html() + "</div>";
Shadowbox.open({
options: {
enableKeys: false
},
player: 'html',
content: elem.innerHTML
});
});
};
但后来我想,与其每次调用 loadEntry 时都让 thisPage 加载 thatPage,我只需在加载 thisPage.js 时调用它一次,将其包装为包含以下内容的变量:一个 jQuery 对象并像这样访问它:
var $Content = null;
jQuery.get('thatPage.html', function(data){
$Content = jQuery(data).find("#content");
});
function loadEntry(entry){
elem = $Content.find("a[name='" + entry + "']").parent().parent().html();
Shadowbox.open({
options: {
enableKeys: false
},
player: 'html',
content: elem
});
};
第二种方法实际上更有效,还是有其他方法更适合我想要做的事情?欢迎任何建议或一般性意见。
谢谢, 杰恩
I'm a bit new to the Ajax thing and am hoping you all can suggest which of these two methods is most efficient. Both approaches seem to work, but perhaps there is some better way altogether?
thisPage.html wants to display a blob of html drawn from thatPage.html (on the same host) in response to a click on a link. I'm using Shadowbox's html player to display the blob, and I'm using jQuery's get() as a shortcut to the XMLHttpRequest.
In my first solution, thisPage.js contains this function:
function loadEntry(entry){
jQuery.get('thatPage.html', function(data){
var elem = document.createElement("div");
elem.innerHTML = "<div class='entry-to-show'>" + jQuery(data).find("a[name='" + entry + "']").parent().parent().html() + "</div>";
Shadowbox.open({
options: {
enableKeys: false
},
player: 'html',
content: elem.innerHTML
});
});
};
But then I thought, rather than have thisPage load thatPage every time loadEntry is called, I'd just call it once when thisPage.js is loaded, wrap it up as a variable containing a jQuery object and access it like this:
var $Content = null;
jQuery.get('thatPage.html', function(data){
$Content = jQuery(data).find("#content");
});
function loadEntry(entry){
elem = $Content.find("a[name='" + entry + "']").parent().parent().html();
Shadowbox.open({
options: {
enableKeys: false
},
player: 'html',
content: elem
});
};
Is this second method actually more efficient, or is there some other approach that would be more appropriate for what I'm trying to do? Any advice or general observations would be welcome.
Thanks,
jjon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的第二种方法肯定更有效。如果不更多地了解您通过从一个页面加载信息到另一个页面实际上试图解决什么问题,我无法提供更好的解决方案,尽管我无法想到您正在做的任何情况都是不合适的解决方案。
Your second approach is definitely more efficient. Without knowing more about what problem you're actually trying to solve by loading information from one page to another I can't provide a better solution although I can't think of any instances where what you're doing would be an inappropriate solution.