我想對以下 html 進行分組:
<h2>Summarize the problem<h2>
<h3>Include details about your goal<h3>
<h3>Include details about your goal<h3>
<h3>Include any error messages<h3>
<h2>Describe What you have tries<h2>
<h3>Show what you’ve tried and tell us what you found<h3>
<h3>Lorem Ipsum LOL<h3>
...
進入
<div>
<h2>Summarize the problem</h2>
<h3>Include details about your goal</h3>
<h3>Include details about your goal</h3>
<h3>Include any error messages</h3>
</div>
<div>
<h2>Describe What you have tries</h2>
<h3>Show what you’ve tried and tell us what you found</h3>
<h3>Lorem Ipsum LOL</h3>
</div>
我試過的
我嘗試遍歷所有元素,但從未奏效。我沒有把代碼放在這里,因為我發現它甚至沒有任何意義
uj5u.com熱心網友回復:
這是一個作業版本。
$("h2").each(function() {
const $this = $(this)
const $h3 = $this.nextUntil("h2"); // find all tags until next H2
$this
.wrap('<div />')
.after($h3)
})
.wrapper { border: 1px solid black }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2>Summarize the problem</h2>
<h3>Include details about your goal</h3>
<h3>Include details about your goal</h3>
<h3>Include any error messages</h3>
<h2>Describe what you have tried</h2>
<h3>Show what you've tried and tell us what you found</h3>
<h3>Lorem Ipsum LOL</h3>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/373268.html
標籤:javascript html 查询
