在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;
});