第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

Dom替換replaceWith()和replaceAll()中,兩次點擊后,<p>第二段</p>和<p>第三段</p>都被替換掉,這不是bug嗎?怎么改成只能替換掉<p>第二段</p>?

<!DOCTYPE html>
<html>

<head>
??? <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
??? <title></title>
??? <script src="http://idcbgp.cn/static/lib/jquery/1.9.1/jquery.js"></script>
??? <style>

??? .right div {
??????? background: yellow;
??? }
??? </style>
</head>

<body>
??? <h2>replaceWith()和replaceAll()</h2>
??? <div class="left">
??????? <button class="bt1">點擊,通過replaceWith替換內(nèi)容</button>
??????? <button class="bt2">點擊,通過rreplaceAll替換內(nèi)容</button>
??? </div>
??? <div class="right">
??????? <div>
??????????? <p>第一段</p>
??????????? <p>第二段</p>
??????????? <p>第三段</p>
??????? </div>
??????? <div>
??????????? <p>第四段</p>
??????????? <p>第五段</p>
??????????? <p>第六段</p>
??????? </div>
??? </div>
??? <script type="text/javascript">
??? //只克隆節(jié)點
??? //不克隆事件
??? $(".bt1").on('click', function() {
??????? //找到內(nèi)容為第二段的p元素
??????? //通過replaceWith刪除并替換這個節(jié)點
??????? $(".right > div:first p:eq(1)").replaceWith('<a style="color:red">replaceWith替換第二段的內(nèi)容</a>')
???? ?
??? })
??? </script>
??? <script type="text/javascript">
??? //找到內(nèi)容為第六段的p元素
??? //通過replaceAll刪除并替換這個節(jié)點
??? $(".bt2").on('click', function() {
??????? $('<a style="color:red">replaceAll替換第六段的內(nèi)容</a>').replaceAll('.right > div:last p:last');
??? })
??? </script>
</body>

</html>

正在回答

1 回答

<!DOCTYPE html>
<html>

<head>
? ?<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
? ?<title></title>
? ?<script src="http://idcbgp.cn/static/lib/jquery/1.9.1/jquery.js"></script>
? ?<style>

? ? ? ?.right div {
? ? ? ? ? ?background: yellow;
? ? ? ?}
? ?</style>
</head>

<body>
<h2>replaceWith()和replaceAll()</h2>
<div class="left">
? ?<button class="bt1">點擊,通過replaceWith替換內(nèi)容</button>
</div>
<div class="right">
? ?<div>
? ? ? ?<p>第一段</p>
? ? ? ?<p>第二段</p>
? ? ? ?<p>第三段</p>
? ?</div>
? ?<div>
? ? ? ?<p>第四段</p>
? ? ? ?<p>第五段</p>
? ? ? ?<p>第六段</p>
? ?</div>
</div>
<script type="text/javascript">
? ?//只克隆節(jié)點
? ?//不克隆事件
? ?$(".bt1").on('click', function() {
? ? ? ?//找到內(nèi)容為第二段的p元素
? ? ? ?//通過replaceWith刪除并替換這個節(jié)點
? ? ? ?$(".right div:first p:eq(1)").replaceWith('<p ?style="color:red">replaceWith替換第二段的內(nèi)容</p>')

? ?})
</script>
</body>
</html>


同學(xué),?$(".right > div:first p:eq(1)").replaceWith('<a style="color:red">replaceWith替換第二段的內(nèi)容</a>')

你把p元素替換成啦a元素,那么第一個div里面只有2個p元素啦,之前第3個p元素那就變成第二p元素,所以當(dāng)你連續(xù)點擊2次時,<p>第二段</p>和<p>第三段</p>都被替換掉。

<div>

? ? ? <p>第一段</p>
? ? ? ?<a>第二段</a>
? ? ? ?<p>第三段</p>
? ?</div>

8 回復(fù) 有任何疑惑可以回復(fù)我~
#1

Spiner_0 提問者

哦,明白了,謝謝
2018-03-17 回復(fù) 有任何疑惑可以回復(fù)我~
#2

web_東 回復(fù) Spiner_0 提問者

點個贊嗎!?
2018-03-17 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

Dom替換replaceWith()和replaceAll()中,兩次點擊后,<p>第二段</p>和<p>第三段</p>都被替換掉,這不是bug嗎?怎么改成只能替換掉<p>第二段</p>?

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號