Html 在ajax中用href替换URL
使用以下代码显示另一页中的文本Html 在ajax中用href替换URL,html,ajax,Html,Ajax,使用以下代码显示另一页中的文本 $.ajax({ url: 'topic/ginger.php', success: function (data) { data = $(data).find('div#display'); $('.res h2').html(data); } }); <a href="topic/
$.ajax({
url: 'topic/ginger.php',
success: function (data) {
data = $(data).find('div#display');
$('.res h2').html(data);
}
});
<a href="topic/ginger.php">
<div class="res" id="content" style="background-color:rgb(50, 190, 166)">
<h2></h2>
</div>
</a>
正如您所看到的,
是ajax代码中的url,现在我有了另外两个具有不同href的div,url
我该怎么做
$('.res').each(function(){
getAjax($(this).find("h2"), $(this).parent().attr('href'));
});
function getAjax(child, link)
{
$.ajax({
url: link,
success: function (data) {
data = $(data).find('div#display');
child.html(data);
}
});
}
假设您的所有div都在res类中,我认为这样就可以了,在res类中循环并获取其中的h2和其父类的href,然后将其传递给ajax调用来完成这项工作。不过我还没有测试过
EDITL刚刚看到了你的最新编辑,你不能有多个ID为=“content”的项目,所以把它们扔掉,给它们每个class=“res”你能展示更多的代码吗,这真的不足以理解你想要什么。我不明白你的问题。URL不同吗?在哪个div中?对问题进行了一些编辑这不会显示来自外部页面的文本,而不在
ajax
中输入topic/ginger.php
到URL
,我如何获得href
值以放置到URL
自动我不确定我提供的示例中href作为“链接”是什么意思变量,因此当放入url区域时,它将充当“topic/ginger.php”。假设您的ajax请求和PHP设置正确,那么应该将该数据放在h2中。看到我做的这个小小提琴,我跳过Ajax调用,因为我没有PHP代码,但是你可以看到每个div的H2都被链接替换了,你需要在中间填充Ajax。
$('.res').each(function(){
getAjax($(this).find("h2"), $(this).parent().attr('href'));
});
function getAjax(child, link)
{
$.ajax({
url: link,
success: function (data) {
data = $(data).find('div#display');
child.html(data);
}
});
}