javascript - 使用 jquery 将文本更改为 h1?

标签 javascript jquery html jquery-selectors

如何将 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/

相关文章:

javascript - 升级 jquery 后表单发布警告消息

javascript - 如何使用 Chrome 的开发工具更改局部变量的值?

javascript - 想要在 javascript 中传递包含类对象的 ArrayList

jquery - 问题 | Jquery插件创建问题

html - 文本对齐:居中;不适用于 Inputfields Bootstrap

javascript - 将javascript Integer转换为字节数组并返回

javascript - 将附加 HTML 的属性设置为 jquery 中的变量?

使用 Gridster 添加网格时出现 Javascript 错误

html - 带有 Bootswatch 主题的 Jasny bootstrap 不起作用?

javascript - CSS3 边框半径和 Internet Explorer 8