使用外部链接自动加载页面+ ajax调用(使用Jquery)

时间:2011-12-02 12:57:51

标签: jquery ajax

当点击侧边栏中的链接时,我有一个页面可以加载信息段。

http://www.youarehere.im/other-financial-services.html

一切正常。

我还想从index.html文件链接到此页面,但是自动加载相关部分,而不是默认状态。

所以在主页上,“accounts”链接加载“other-financial-services.html”AND自动从服务器请求ajax调用“accounts”链接。

有什么想法吗?我知道这个Jquery片段是完全错误的,但我对前端脚本很新,我在黑暗中拍摄!

HTML:

<div class="box accounting"><h2><a id="service_accounts">Accounting</a></h2></div>

JQuery的:

$(document).ready(function() {
    $.ajaxSetup({
        cache: false
    });

    var ajax_load = "<img src='img/load.gif' alt='loading...' />";
    var load_services = "other-financial-services.html";
    var accounts = "accounts.php";

    $('#service_accounts').click(function() {
        $.get(load_services).load(accounts);
    });
});

3 个答案:

答案 0 :(得分:1)

我认为你应该这样做:

<div class="box accounting"><h2><a id="service_accounts">Accounting</a></h2></div>
<div id='target'></div>

$('#service_accounts').click(function(){
    //load the html page int the div with id=target (of course is just an example)
    $('#target').load(load_services);
    $.get(accounts, function(data){
        //do something with data returned from the server
     });
});

答案 1 :(得分:0)

如果我理解正确,您希望根据打开页面的位置更改这些变量吗?

var load_services = "other-financial-services.html";
var accounts = "accounts.php";

这意味着你需要找到一种方法将参数传递给这个页面,这样你就可以在值之间切换。 不幸的是,我缺乏任何不错的PHP知识,但我认为应该可以添加

?ajaxloadselector=1 

到您的网址?

然后您可以在链接到此页面的页面中编辑URL,并在此页面上,根据传递的'ajaxloadselector'参数创建一个填充变量的开关。

答案 2 :(得分:0)

在主页上,如果要链接到“金融服务”页面的“会计”子菜单选项,请创建如下链接:< / p>

http://www.youarehere.im/other-financial-services.html#accounts

和js代码类似于

var url = document.location.toString();
if (url.match('#')) { // the URL contains an anchor
  // click the navigation item corresponding to the anchor
  var anchor = '#' + url.split('#')[1];
  $('#load_' + anchor).click();
}

因此,只要您在网址中加载#<something>的网页,就会点击元素#load_<something>

代码从http://blog.rebeccamurphey.com/2007/12/04/anchor-based-url-navigation-with-jquery(已修改)无耻地被盗

相关问题