我試圖包裝div容器中的一些HTML內容,然後追加跨度到該容器。但我無法弄清爲什麼附加跨度不會出現。jQuery包裝和appendTo不會導致預期的結果
所以我很好奇,如果我做錯了什麼。
$('button').click(function() {
\t
var $container = $('<div class="container"></div>');
\t $('.target').wrap($container);
\t \t \t
$label = $('<span class="duration-label">Hi there</span>');
\t $label.appendTo($container);
});
.target {
border: 1px solid black;
}
.container {
background: red;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div class="target">
<span>This is some unimportant text</span>
</div>
<button type="button">Click me</button>
我不知道你是否注意到我的答案,只是爲了讓你知道我解釋了爲什麼'wrap'以這種方式工作的,你想知道的情況下... – Amit
@Amit我感謝。我剛剛選擇了T.J. Crowder的答案是第一位的。兩個答案都是相似的,都是對的。 – Behnil
這很好,我想提請你注意那裏的額外信息。我希望這能幫助你進一步理解這個問題。 – Amit