我知道如何使用 jquery 将完整的 html 页面加载到 div 中,但是如何从页面中只加载一个 div 呢?这是我的脚本:
$('.open-alert,.help').click(function(e) {
e.preventDefault();
e.stopPropagation();
$(".overlay").fadeIn();
$('.overlay').load(this.href, function() {});
$("body").addClass("masked");
})
}
当用户单击带有帮助类的链接时,我希望它加载具有特定 id 的 div。
因此链接如下所示:
<a class="help" href="help.htm#help-one">Help message one</a>
应该从 help.htm 加载这个 div:
<div id="help-one">Help message one</div>
但这似乎不起作用。它加载整个页面。我做错了什么?
最佳答案
我会稍微改变你的代码......
$('.open-alert,.help').click(function (e) {
var $this = $(this),
target = $this.attr('data-target'),
$(".overlay").fadeIn();
$('.overlay').load(target);
$("body").addClass("masked");
});
和 html:
<a class="help" data-target="help.htm #help-one" href="javascript:void(0);">Help message one</a>
注意data-target
中的空格...这很重要。此外,这样做可以让您更清楚地了解单击链接时代码的用途。
关于javascript - 从 href jquery 加载特定的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30739260/