DOM操作在模板文字中不起作用

问题描述 投票:0回答:1

开始DOM:

var group = document.getElementsByClassName('form-group');
var p = group[0].getElementsByTagName('p');

没有工作代码:

for (var i = 0; i <= p.length; i++) {
`this is : ${p[i].innerHTML}`;}

工作代码:必须这样做才能访问innerHTML:

for (var i = 0; i <= p.length; i++) {
console.log("this is : "+ p[i].innerHTML +"")}

实际代码很长,因为我想从不同的标签循环多个innerHTML。由于我无法通过模板文字格式访问,因此必须执行典型的console.log方式。

是真的不可能还是我错过了那里的东西吗?

完整HTML:

<div class="form-group">
    <div class="mb-1">
        <h4 class="mb-3" style="font-family: Rubik, Lato, sans-serif; color: rgb(99, 57, 145); font-size: 20px;">Where i can use Prepay to secure my payment?</h4>
        <p class="light-font mb-5" style="color: rgb(149, 144, 148); font-family: Rubik, Lato, sans-serif; font-size: 16px;">Prepay works just like online banking, you can send to anyone, anywhere like online marketplace (mudah,carousell etc), online gaming and when you dealing with online services where you need to pay your money in advance.</p>

        <h4 class="mb-3" style="font-family: Rubik, Lato, sans-serif; color: rgb(99, 57, 145); font-size: 20px;">My shipment was lost/damaged during delivery by Prepay Courier, What should I do?</h4>
        <p class="light-font mb-5" style="color: rgb(149, 144, 148); font-family: Rubik, Lato, sans-serif; font-size: 16px;">Every courier delivery has been insured with amount up to RM200, contact us to proceed for a claim.</p>

        <!-- TOO LONG, I JUST CUT THESE 2 ABOVE -->
    </div>
</div>
javascript dom
1个回答
0
投票

i <= p.length;更改为i < p.length;

var group = document.getElementsByClassName('form-group');
var p = group[0].getElementsByTagName('p');


for (var i = 0; i < p.length; i++) {
  console.log(`this is : ${p[i].innerHTML}`);
}
<div class="form-group">
  <div class="mb-1">
    <h4 class="mb-3" style="font-family: Rubik, Lato, sans-serif; color: rgb(99, 57, 145); font-size: 20px;">Where i can use Prepay to secure my payment?</h4>
    <p class="light-font mb-5" style="color: rgb(149, 144, 148); font-family: Rubik, Lato, sans-serif; font-size: 16px;">Prepay works just like online banking, you can send to anyone, anywhere like online marketplace (mudah,carousell etc), online gaming and when you dealing with online services where you need to pay your money in advance.</p>

    <h4 class="mb-3" style="font-family: Rubik, Lato, sans-serif; color: rgb(99, 57, 145); font-size: 20px;">My shipment was lost/damaged during delivery by Prepay Courier, What should I do?</h4>
    <p class="light-font mb-5" style="color: rgb(149, 144, 148); font-family: Rubik, Lato, sans-serif; font-size: 16px;">Every courier delivery has been insured with amount up to RM200, contact us to proceed for a claim.</p>


  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.