我有一个帐户页面,其中包含使用.load加载页面的链接。我想知道如何从另一个页面(而不是从帐户页面)访问加载帐户信息页面?另一个页面只有一个到account.html的常规链接,如果已加载,那么我没有看到load-account-information.html选项卡。我能以某种方式在URL中传递函数loadAccountInfomration吗?谢谢!
帐户页面
function loadAccountInformation() {
$('#accountMain').load("load-account-information.html");
}
<a href="#" onclick="loadAccountInformation()">My Information</a>
其他页面
<a href="account.html">Account Information</a>
帐户页面上有多个加载功能,我只想知道我是否可以在另一个页面的点击中特别运行一个。
答案 0 :(得分:4)
如果要从另一个页面上的链接运行特定脚本,可以使用哈希标记。在{URL>的末尾附加#load-account-info
之类的内容:
<a href="account.html#load-account-info">Account Information</a>
然后在account.html
:
$(function() {
// show account info if #showAccountInfo is present
showAccountInfo();
// detect url hash changes and fire off showAccountInfo
$(window).bind("hashchange", showAccountInfo());
});
function showAccountInfo() {
if (window.location.hash == "load-account-info")
loadAccountInformation();
}
注意:并非所有浏览器都支持“hashchange”事件。因此,有一个jQuery plugin可以增加支持。
修改:添加了对检测同一页面点击次数的支持。
来源:Working with single page websites and maintaining state using a URL hash and jQuery,On - window.location.hash - Change?
答案 1 :(得分:1)
听起来你试图从ajax插入的内容中运行ajax调用。
在jquery中使用ajax很酷的事情是回调。加上它非常容易使用。
function loadAccountInformation() {
$('#accountMain').load("load-account-information.html", function () {
var ele = $(this);
ele.find('a').bind('click', function(){
ele.load('url');
})
});
}
<a href="#" onclick="loadAccountInformation()">My Information</a>
如果这就是你想要的东西,那么我建议你用这样的东西来模拟一点
function loadInfo(url){
$('#accountMain').load(url, function () {
attachEvents();
} )
}
function attachEvents(){
//Add In selector or selectors that can target ajax links
$('#linkArea').find('a').unbind('click').bind('click' function(){
var url = this.href;
loadInfo(url);
})
}
答案 2 :(得分:0)
JavaScript在浏览器中运行,因此您无法远程调用它。您需要访问“其他页面”上的相同脚本,并将loadAccountInformation函数作为点击处理程序添加到链接中,方法与在帐户页面上执行相同。
如果您希望在从其他页面到达帐户页面后运行该功能,则可以在页面加载后调用该功能。有点像...
$(function () {
loadAccountInformation();
});