簡體   English   中英

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

[英]DOM Manipulation not working inside Template Literals

啟動 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>

改變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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM