开发者

jQuery - loading content into DIV, links not working in Safari

I have an issue with jQuery and Safari (5.03). Hope this makes sense.

I am using the code below to load a page into a DIV. On the parent page I have some code that selects all links in the content that is loaded and tells this to the links (when clicked) into the same DIV. This works fine in IE, 开发者_如何学JAVAFirefox, and Chrome, but I cannot get it to work in Safari. resultsDisplay is the class of all the links in in the content that is loaded. Does this make any sense? How can I can the links in the loaded content to load into that same div on the parent page (#results).

  jQuery(document).ready(function(){
$(".resultsDisplay a").click(function() {
  $.ajax({
   url: $(this).attr("href"),
   success: function(msg){
     $("#results").html(msg);
   }
 });
 return false;
});
});

Here is the page I referring to:

http://www.fishur.com

The content reloads as a new page in the browser rather than inside the DIV. Click on one of this fish names is IE/FireFox/Safari and you'll see what I mean - then click one of the names in Safari.


UPDATE

I see the real problem here. You are loading those elements dynamically, while you are only setting the event handlers at document onload. The elements don't exist when this code is executed. To solve this problem use jQuery's live() handlers or re-run the init after the load of that section.


Don't put the url in the href element. Instead use something else. The browser is opening the href when the url is clicked.

Another option: Don't use an <a> tag for these buttons.

Another option: Use the jQuery event.preventDefault();


jQuery(document).ready(function(){
$(".resultsDisplay a").click(function(event) {
    event.preventDefault();
    $.ajax({
        url: $(this).attr("href"),
        success: function(msg){
            $("#results").html(msg);
        }
    });
    return false;
});
});

You have to prevent the default action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜