jQuery-从另一个页面运行一个函数 [英] jQuery - run a function from another page
问题描述
我有一个带有链接的帐户页面,这些链接用.load加载页面.我想知道如何从另一个页面(而不是从帐户页面)访问加载帐户信息页面吗?另一页只是有一个指向account.html的常规链接,如果已加载,则看不到load-account-information.html选项卡.我可以通过某种方式在URL中传递函数loadAccountInfomration吗?谢谢!
I have a account page with links that load the pages with the .load. I am wondering how I can get to the load account info page from another page (not from the account page)? The other page just has a regular link to account.html and if that is loaded then I don't see the load-account-information.html tab. Can I pass the function loadAccountInfomration in the URL somehow? Thanks!
帐户页面
function loadAccountInformation() {
$('#accountMain').load("load-account-information.html");
}
<a href="#" onclick="loadAccountInformation()">My Information</a>
其他页面
<a href="account.html">Account Information</a>
帐户页面上有多个加载功能,我只想知道我是否可以在另一个页面上的点击中运行一个.
There are multiple load functions on the account page and I just want to know if I can run one in particular on a click from another page.
推荐答案
如果要在一页上通过另一页上的链接运行特定脚本,则可以使用哈希标签.在URL末尾附加#load-account-info
之类的内容
If you want to run specific scripts on one page from the link on another, you could use hash tags. Append something like #load-account-info
to the end of the URL:
<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 插件.
Note: not all browsers support the "hashchange" event. As such, there is a jQuery plugin that adds support.
修改:添加了检测同一页面上点击的支持.
Edit: added support for detecting clicks on the same page.
来源:使用单页网站并使用URL哈希和jQuery维护状态 ,在-window.location.hash-更改?
Sources: Working with single page websites and maintaining state using a URL hash and jQuery, On - window.location.hash - Change?
这篇关于jQuery-从另一个页面运行一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!