[英]$.ajax() in success doesn't update div with div from another file (the same id name)
我有兩個文件html,都具有同名id = example的div。 我想使用$ .ajax()從第二個文件加載內容,並將當前文本放在div中的第一個文件中。 問題是$(data).find('#example')不返回元素#example。 我知道此錯誤是因為我使用相同的ID名稱,但畢竟我想從另一個文件下載該div,所以我不明白。 誰能解釋?
index.html:
<nav>
<ul>
<li><a href="index.html">home</a></li>
<li><a href="contact.html">contact</a></li>
</ul>
</nav>
<div id="example">one one one one </div>
contact.html:
<nav>
<ul>
<li><a href="index.html">home</a></li>
<li><a href="contact.html">products</a></li>
</ul>
</nav>
<div id="example">one one one one </div>
$('nav a').on('click', function(e){ e.preventDefault(); var url = this.href; $.ajax({ type: "POST", url: url, success: function(date){ $('#example').html($(date).find('#example')); } }); });
$('nav a').on('click', function(e){ e.preventDefault(); var url = this.href; $.ajax({ type: "POST", url: url, success: function(response){ $('#example').html($(response.data).find('#example')); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.