例如我有一个 <p>
标签包含一些带有 <em>
的句子像下面这样
<p>There's <em>a</em> script that he's
written that is <em>a</em> really cool Batman idea.</p>
我使用一些 javascript 向所有 em 元素添加一个类
$("em").addClass("Keyword-Result");
现在我想知道如何打印所有 <em class="Keyword-Result")
喜欢标签吗?
最佳答案
但是这里不需要动态添加类,但这里是代码片段,它还将标签附加到#tags:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>There's <em>a</em> script that he's
written that is <em>a</em> really cool Batman idea.</p>
<div id="tags"></div>
<style>
em.Keyword-Result {
background-color: grey;
padding: 4px;
border-radius: 2px;
margin:2px;
color:#fff;
}
</style>
<script>
$(document).ready(function(){
$("em").clone().addClass("Keyword-Result").appendTo('#tags');
});
</script>
关于javascript - 从 <p> 中获取所有 <em> 并将它们像标签一样输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37021724/