jQuery 地址插件帮助

发布于 2024-10-19 22:35:25 字数 897 浏览 5 评论 0原文

我在弄清楚如何使用 jQuery 地址插件时遇到了一些困难 - http://www. asual.com/jquery/address/- 以我有限的 jQuery/javascript 知识,我会认真挖掘一些帮助!

到目前为止,我的代码如下所示:

jQuery(function($) {
   $.ajaxSetup({ cache: false });
   var hijax = $('ul.hijax a');
   var loader = $('<div id="spinner"></div>');
   var container = $('#ajax-container');
   hijax.address(function() {
     dis = $(this);
     hijax.removeClass('ajax-on');
     dis.addClass('ajax-on');
     var url = dis.attr('href') + ' #biog-container';
     container.html(loader).load(url);
     return dis.attr('id');
   });
});

我希望这是不言自明的。我有一个指向页面的 ul 链接,我在其中通过 AJAX 加载 div 的内容,idbiog-container< /代码>。 AJAX 工作正常,并且 url 会使用链接的 id 进行更新,但是当我单击后退按钮时,url 会发生变化,但内容保持不变。

有什么想法吗,我是不是很傻?!

I'm having a little difficulty working out how to use the jQuery address plugin - http://www.asual.com/jquery/address/- with my limited jQuery/javascript knowledge and would seriously dig some help!

So far my code looks like this:

jQuery(function($) {
   $.ajaxSetup({ cache: false });
   var hijax = $('ul.hijax a');
   var loader = $('<div id="spinner"></div>');
   var container = $('#ajax-container');
   hijax.address(function() {
     dis = $(this);
     hijax.removeClass('ajax-on');
     dis.addClass('ajax-on');
     var url = dis.attr('href') + ' #biog-container';
     container.html(loader).load(url);
     return dis.attr('id');
   });
});

Which I'm hoping is fairly self-explanitory. I have an ul of links to pages where I load via AJAX the contents of a div with an id of biog-container. The AJAX works ok and the url is updated with the ids of the links, but when I click on the back button, the url changes, but the content remains the same.

Any thoughts, am I being stupid?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-10-26 22:35:25

老实说,address 方法并不会为您做太多工作。实际上,该插件不会自动添加支持。

然而,做起来非常简单。您只需为 change 方法设置处理程序:

$.address.change(function(e){
    // The function receives a single eventobject parameter that contains the 
    // following properties: 
    //    value, path, pathNames, parameterNames, 
    //    parameters, queryString

    /* Do something based on e */
});

当地址更改时,您的函数就会被触发。您还可以将其他事件 internalChangeexternalChange 设置为在内部或外部发生地址更改时触发。


编辑:只是为了扩展上面的内容:

这是一个完整的工作示例。换句话说,您不需要在插件中调用任何其他内容即可使其工作:

// Override the click events of links
$("a").click(function(e) {
    e.preventDefault();
    $.address.path(this.id);
});

// Method called with the address changes
$.address.change(function(e) {
    // This is called when the 
    $("#content").html("Something based on " + e.value);
});

单击链接时,它将地址路径(就插件而言)设置为链接的 ID。这会生成一个动作,以便后退/前进正常工作。它不会更改链接本身,因此如果未启用 JavaScript,它将转到链接地址。

$.address.change 位设置处理程序。这是您加载内容的位置。在我的示例中,它使用 e.value 来获取地址路径的值。因此,这将类似于 /link1/link2 等。当链接在内部(由插件)或外部(由浏览器返回)更改时,会调用此处理程序/向前)。

注意:我注意到这在 jQuery 1.5.1 中不能完美地工作。我很确定这与 jQuery 1.5 属性选择器(地址插件似乎使用的)的更改有关。不过 jQuery 1.4 工作得很好。

To be honest, the address method doesn't do too much in the way of work for you. Actually, the plugin doesn't add support automatically.

However, it's pretty simple to do. You just have to setup the handler for the change method:

$.address.change(function(e){
    // The function receives a single eventobject parameter that contains the 
    // following properties: 
    //    value, path, pathNames, parameterNames, 
    //    parameters, queryString

    /* Do something based on e */
});

When the address is changed, your function gets fired. There are also other events internalChange and externalChange that you can set up to fire when the address change occurs internally or externally.


Edit: Just to expand on the above:

This is a full blown example of it working. In other words, you don't need to call anything else in the plugin for it to work:

// Override the click events of links
$("a").click(function(e) {
    e.preventDefault();
    $.address.path(this.id);
});

// Method called with the address changes
$.address.change(function(e) {
    // This is called when the 
    $("#content").html("Something based on " + e.value);
});

When the links are clicked, it sets the address path (in terms of the plugin) to the ID of the link. This generates an action so that the back/forward works correctly. It doesn't change the link itself so if JavaScript isn't enabled it will go to whatever the link address was.

The $.address.change bit sets the handler. This is where you would load the content. In my example, it uses e.value to get the value of the address path. So that would be something like /link1, /link2, etc. This handler gets called when the link is changed internally (by the plugin) or externally (by the browser back/forward).

Note: I noticed that this doesn't work flawlessly with jQuery 1.5.1. I'm pretty sure it has something to do with the change to jQuery 1.5 attribute selectors (which the address plugin seems to use). jQuery 1.4 works fine though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文