[英]Change text to h1 with jquery?
How do I make the 2990 kr
text into an h1
? 如何将2990 kr
文本转换为h1
?
<div class="productPrice">
<span>2 990 kr</span>
</div>
Alt 1 http://api.jquery.com/html/ Alt 1 http://api.jquery.com/html/
$(".productPrice").html("h1");
Alt 2 http://api.jquery.com/text/ Alt 2 http://api.jquery.com/text/
$(".productPrice").text("h1");
使用.wrapAll()
:
$('.productPrice span').wrapAll('<h1></h1>');
.wrapInner()
method can be used.可以使用 .wrapInner()
方法。
Wrap an HTML structure around the content of each element in the set of matched elements.围绕匹配元素集中每个元素的内容包装HTML结构。
$('.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>
The above solution will produce, <span><h1>...</h1></span>
which is invalid. 上述解决方案将生成<span><h1>...</h1></span>
,这是无效的。 Instead use wrap()
而是使用wrap()
Wrap an HTML structure around each element in the set of matched elements. 围绕匹配元素集中的每个元素包装HTML结构。
$('.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>
While this is easily possible with jQuery: 虽然使用jQuery很容易实现这一点:
// selecting the '.productPrice' elements, and
// and wrapping the inner content (to avoid
// creating an <h1> element within a <span>:
$('.productPrice').wrapInner('<h1></h1>');
This is also – of course – possible with plain JavaScript: 当然,这也可以使用纯JavaScript:
// creating a function that takes two arguments,
// toWrap: the Node whose contents should be wrapped,
// wrapWith: the element-type with which those contents
// should be wrapped:
function wrapInner(toWrap, wrapWith) {
// retrieving the contents of the element to wrap:
var contents = toWrap.childNodes,
// the newly-created element type:
newElem = document.createElement(wrapWith);
// inserting the new element before the first of the
// the node's childNodes:
toWrap.insertBefore(newElem, contents[0]);
// while contents exist:
while (contents.length) {
// move the first of those contents into the
// new element:
newElem.appendChild(contents[0]);
}
}
// retrieving the '.productPrice' elements with
// document.querySelectorAll(); and converting
// Array-like NodeList into an Array, using
// Array.from():
var elements = Array.from( document.querySelectorAll('.productPrice') );
// iterating over the array of elements, using
// Array.prototype.forEach():
elements.forEach(function (el) {
// calling the function, passing the node
// and the string for the replacement-element:
wrapInner(el, 'h1');
});
function wrapInner(toWrap, wrapWith) { var contents = toWrap.childNodes, newElem = document.createElement(wrapWith); toWrap.insertBefore(newElem, contents[0]); while (contents) { newElem.appendChild(contents[0]); } } var elements = Array.from(document.querySelectorAll('.productPrice')); elements.forEach(function(el) { wrapInner(el, 'h1'); });
<div class="productPrice"> <span>2 990 kr</span> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.