我想包装一个<input type="text">
就在文字周围hello world
。 Span
不应该被它包围。
<div class="outer">
"hello world"
<span class="inner">Inner</span>
</div>
$('.inner').parent().contents().wrap('<input type="text"/>')
这个将输入包裹在文本和跨度周围。我想避免跨度。谁能帮帮我吗。 fiddle
最佳答案
您无法将文本字段包裹在某些内容周围,您需要
var el = $('.inner')[0].previousSibling;
$('<input />').val(el.nodeValue.trim()).insertBefore('.inner');
$(el).remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer">
"hello world"
<span class="inner">Inner</span>
</div>
关于javascript - 将 div 包裹在文本周围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32879288/