如何将 2990 kr
文本变成 h1
?
<div class="productPrice">
<span>2 990 kr</span>
</div>
替代 1 http://api.jquery.com/html/
$(".productPrice").html("h1");
备选方案 2 http://api.jquery.com/text/
$(".productPrice").text("h1");
最佳答案
.wrapInner()
可以使用方法。
Wrap an HTML structure around the content of each element in the set of matched elements.
$('.productPrice span').wrapInner('<h1 />')
$('.productPrice span').wrapInner('<h1 />')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="productPrice">
<span>2 990 kr</span>
</div>
上面的解决方案将产生,<span><h1>...</h1></span>
这是无效的。而是使用 wrap()
Wrap an HTML structure around each element in the set of matched elements.
$('.productPrice span').wrap('<h1 />')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="productPrice">
<span>2 990 kr</span>
</div>
关于javascript - 使用 jquery 将文本更改为 h1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37784908/