【发布时间】:2019-03-20 16:34:59
【问题描述】:
我有这段 JQuery 代码需要用 Vanilla JS 重写。
$('#myID :not(a, span)').contents().filter(
function() {
return this.nodeType === 3 && this.data.trim().length > 0;})
.wrap('<span class="mySpanClass" />');
我试过了;
Array.prototype.forEach.call(document.querySelectorAll('#myID :not(a), #myID :not(span)'),
function(el, i){
var span = document.createElement('span');
span.setAttribute('class', 'rvReaderContent');
while((el.firstChild) && (el.firstChild.nodeType === 3) && (el.firstChild.data.trim().length > 0)){
var textNode = el.firstChild
el.insertBefore(span, textNode);
span.appendChild(textNode);
}
});
我尝试了其他变体,但似乎没有任何效果。我找不到 JQuery 的 contents() 方法的替代品。
非常感谢您对此的帮助。谢谢。
【问题讨论】:
-
嗨。已经检查了建议的文章,它似乎没有回答我的问题。
标签: javascript jquery equivalent