I am displaying multiple dynamic links which use the same , ajaxloads the content on the first link fine but doesn't work for the rest of them. How can I get it to load the content of other links in the same div?
我正在显示多个使用相同的动态链接,ajax加载第一个链接上的内容很好,但不适用于其余的。如何让它在同一个div中加载其他链接的内容?
Html:
HTML:
$string .= '<a id="hrefid" data-id="'.$name["id"].'" href="link" >'.$name["name"].'</a>'.
<div id="content"> </div>
Jquery:
jQuery的:
$('#hrefid').on('click', function (e) {
var load = $(e.target).attr("href");
if(load == "#link") {
$.ajax({
type: 'post',
url: "/page/test/"+id,
complete: function (event) {
$("#content").contents().remove();
$("#content").append(event.responseText);
}
});
}
});
1 个解决方案
#1
3
Change the id to a class, remove # from the if statement
将id更改为类,从if语句中删除#
$string .= '<a class="hrefid" data-id="'.$name["id"].'" href="link" >'.$name["name"].'</a>'.
<div class="content"> </div>
$('.hrefid').on('click', function (e) {
var el = $(this);
var load = $(e.target).attr("href");
if(load == "link") {
$.ajax({
type: 'post',
url: "/page/test/"+id,
success: function (event) {
el.next().empty();
el.next().append(event.responseText);
}
});
}
});
#1
3
Change the id to a class, remove # from the if statement
将id更改为类,从if语句中删除#
$string .= '<a class="hrefid" data-id="'.$name["id"].'" href="link" >'.$name["name"].'</a>'.
<div class="content"> </div>
$('.hrefid').on('click', function (e) {
var el = $(this);
var load = $(e.target).attr("href");
if(load == "link") {
$.ajax({
type: 'post',
url: "/page/test/"+id,
success: function (event) {
el.next().empty();
el.next().append(event.responseText);
}
});
}
});