在Jquery中使用Ajax()函数将外部页面的PART加载到div中 [英] Use Ajax() function in Jquery to load PART of an external page into div

查看:111
本文介绍了在Jquery中使用Ajax()函数将外部页面的PART加载到div中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Ajax / jQuery.ajax函数从外部页面加载DIV元素到我的当前页面。虽然我已成功加载整个外部网页,但我似乎无法加载 DIV元素。

I'm trying to load a DIV element from an external page into my current page using the Ajax/jQuery.ajax function. While I have successfully been able to load an entire external page, I can't seem to load just the DIV element.

这是我的代码:

$("a").click(function() {
  /* grabs URL from HREF attribute then adds an  */
  /* ID from the DIV I want to grab data from    */
  var myUrl = $(this).attr("href") + "#external-div";
  $.ajax( {
  url: myUrl,
  success: function(html) {
    /* loads external content into current div element */
    $("#current-div").append(html);
    }
  });
  return false;
});

它没有任何麻烦地抓取HREF属性,但不会将#external-div网址。任何想法?

It grabs the HREF attribute without any trouble, but won't append "#external-div" to the URL. Any ideas?

非常感谢!

〜Jared Crossley

~Jared Crossley

推荐答案

如果你想返回那个div,你可以使用jQuery的 load 方法来简单地加载返回的内容#current-div ala

If you wanted to just return that div you could use the load method of jQuery to simply load the content returned into your #current-div ala

$("a").click(function() {
  /* grabs URL from HREF attribute then adds an  */
  /* ID from the DIV I want to grab data from    */
  var myUrl = $(this).attr("href") + " #external-div";
  $("#current-div").load(myUrl);
  return false;
});

查看 jQuery Ajax /加载文档

这篇关于在Jquery中使用Ajax()函数将外部页面的PART加载到div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆