使用 Jquery 中的 Ajax() 函数将外部页面的 PART 加载到 div 中 [英] Use Ajax() function in Jquery to load PART of an external page into 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"附加到 URL.有什么想法吗?
It grabs the HREF attribute without any trouble, but won't append "#external-div" to the URL. Any ideas?
非常感谢!
~贾里德·克罗斯利
推荐答案
如果您只想返回那个 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() 函数将外部页面的 PART 加载到 div 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!