我必须更改div内跨度的数据 所有跨度的类都是相同的,所有div的类都是一样。
我试过循环推荐,但它没有做我想要的。我需要找到一种更好的方法而不是循环,因为它也可以在一行中完成,我可以使用JavaScript一次性替换所有跨度的数据。
var testimonials = document.querySelectorAll('.step_input');
Array.prototype.forEach.call(testimonials, function(elements, index) {
console.log(elements);
});

<div class="step_input" contenteditable="true">
The class of div is same
<span class="ing-tag_0"> The data to be changed </span>
<span class="ing-tag_1"> The data not to be changed </span>
</div>
<div class="step_input" contenteditable="true">
This one has same class of span
<span class="ing-tag_0"> The data to be changed </span> only
<span class="ing-tag_0"> The data to be changed </span>
</div>
<div class="step_input" contenteditable="true">
<span class="ing-tag_0"> qweq </span>
</div>
&#13;
答案 0 :(得分:2)
document
.querySelectorAll('.step_input > .ing-tag_0')
.forEach(e => e.innerHTML = 'REPLACED');
<div class="step_input" contenteditable="true">
The class of div is same
<span class="ing-tag_0"> The data to be changed </span>
<span class="ing-tag_1"> The data not to be changed </span>
</div>
<div class="step_input" contenteditable="true">
This one has same class of span
<span class="ing-tag_0"> The data to be changed </span> only
<span class="ing-tag_0"> The data to be changed </span>
</div>
<div class="step_input" contenteditable="true">
<span class="ing-tag_0"> qweq </span>
</div>
注意:querySelectorAll
已经返回Array
。
注意²:这将替换.ing-tag_0
类的直接孩子。如果此类可以包装在另一个元素中并且也应该替换,则只需删除选择器中的>
。
答案 1 :(得分:2)
您可以使用以下代码
var testimonials = document.querySelectorAll('.step_input > .ing-tag_0');
Array.prototype.forEach.call(testimonials, function(elements, index) {
elements.innerHTML = 'new text';
});
<div class="step_input" contenteditable="true">The class of div is same <span class="ing-tag_0" > The data to be changed </span>
<span class="ing-tag_1" > The data not to be changed </span>
</div>
<div class="step_input" contenteditable="true">This one has same class of span<span class="ing-tag_0" > The data to be changed </span> only<span class="ing-tag_0" > The data to be changed </span>
</div>
<div class="step_input" contenteditable="true"><span class="ing-tag_0" > qweq </span></div>
答案 2 :(得分:0)
您可以将选择器修改为var testimonials = document.querySelectorAll('.step_input > .ing-tag_0');
testimonials.forEach(function(spanElem, index) {
spanElem.innerHTML = 'spanElem' + index;
});
。如:
var testimonials = document.querySelectorAll('.step_input > .ing-tag_0');
testimonials.forEach(function(spanElem, index) {
spanElem.innerHTML = 'spanElem' + index;
});
更新以反映OP问题中的新代码
请参阅以下演示:
<div class="step_input" contenteditable="true">
The class of div is same
<span class="ing-tag_0"> The data to be changed </span>
<span class="ing-tag_1"> The data not to be changed </span>
</div>
<div class="step_input" contenteditable="true">
This one has same class of span
<span class="ing-tag_0"> The data to be changed </span> only
<span class="ing-tag_0"> The data to be changed </span>
</div>
<div class="step_input" contenteditable="true">
<span class="ing-tag_0"> qweq </span>
</div>
{{1}}