1 回答

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
我冒昧地在它周?chē)砑恿烁啾尘靶畔ⅲ坪豕ぷ鞯煤芎?。我稍微改變了你的語(yǔ)法,但沒(méi)有改變所討論的主線(xiàn)。有兩個(gè)元素只是為了清楚地表明 .insertAfter() 擊中了正確的元素,而且只有那個(gè)元素。
這是底部帶有腳本的 HTMl。
<html dir="ltr">
<head>
? ? <meta charset="UTF-8" />
? ? <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0"/>
? ? <style>
? ? ? ? body { display: flex; flex-direction: column; }
? ? ? ? ul { display: inline-block; list-style-type: none; text-align: left; background: #ccc; border-radius: 1rem; padding: 1rem; margin: 2rem auto; }
? ? ? ? .inserted { color: green;}
? ? </style>
</head>
<body>
? ? <ul class="productWrapper">
? ? ? ? <li>First</li>
? ? ? ? <li>Second</li>
? ? ? ? <li>Third</li>
? ? ? ? <li>Fourth</li>
? ? ? ? <li>Fifth</li>
? ? ? ? <li>Sixth</li>
? ? </ul>
? ? <ul class="someOtherClass">
? ? ? ? <li>Other First</li>
? ? ? ? <li>Other Second</li>
? ? ? ? <li>Other Third</li>
? ? ? ? <li>Other Fourth</li>
? ? ? ? <li>Other Fifth</li>
? ? ? ? <li>Other Sixth</li>
? ? </ul>
? ? <script src="http://code.jquery.com/jquery-3.4.1.min.js"></script>
? ? <script>
? ? ? ? const $target = $(`ul[class='productWrapper']>li:nth-child(5)`)
? ? ? ? const $test = $(`<li class='inserted'>Inserted after fifth</li>`);
? ? ? ? $test.insertAfter($target);
? ? </script>
</body>
</html>
頁(yè)面完全加載并且腳本完成后,這就是頁(yè)面的外觀,我相信這就是您的想法。
- 1 回答
- 0 關(guān)注
- 138 瀏覽
添加回答
舉報(bào)