用jQuery .load函数刷新一个元素而不复制它 [英] Refresh an element with jQuery .load function without duplicating it
问题描述
当用户点击提交按钮时,div .forum
应该刷新(不刷新页面)。但是,由于某些原因,它会在原始div内创建一个新的div .forum
,例如:
When the user clicks the submit button, the div .forum
is supposed to refresh (without refreshing the page). However, for some reason it instead creates a new div .forum
inside the original div, like ths:
< a href =https://i.stack.imgur.com/sfeo2.png =nofollow noreferrer>
虽然我知道我可以通过在div .forum附近添加一个容器来防止这种情况发生。
,我想知道是否有一个选项,这意味着我不需要一个容器div?
While I am aware that I can prevent this by adding a container around the div .forum
, I was wondering if there's an option which means I don't need a container div?
<div class="forum">
<form id="theforum">
<input type="submit">
</form>
</div>
<script>
$(document).on("submit", "#theforum", function(e) {
e.preventDefault();
$(".forum").load(location.href + " .forum");
});
</script>
推荐答案
我相信它可以解决你的问题:
I can't test if this works right now but i believe it solves your problem:
$(".forum").load(location.href + " .forum > *");
这样您就不会抓住div本身,而是抓住div的内容。
This way you're not grabbing the div itself, but its contents.
这篇关于用jQuery .load函数刷新一个元素而不复制它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!