亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 JavaScript var 添加到頁面上的多個位置

將 JavaScript var 添加到頁面上的多個位置

犯罪嫌疑人X 2024-01-03 16:34:18
我有一些 HTML,其中有 2 個同名標簽,在我的 JavaScript 中我有一個var,這樣將來如果電話號碼發生變化,只需在一處(在 JavaScript 中)進行更改,并將應用于所有標簽與匹配的命名。我似乎不記得如何執行此操作,因為我無法使用“ID”,因為顯示隱藏時會顯示“頁腳” Div。一個是 a <p>,另一個是 a <span>,該頁面包含其中的一些。它需要是 JavaScript 而不是 jQuery。代碼<div class="row m-0 d-flex align-items-center p-5">    <div class="col-12 col-lg-6 pr-lg-0">        <h1>Oops, something went wrong</h1>        <span id="otherErrorTxt" style="display: none">            <p>This error has occurred whilst processing your request.</p>            <p>If the error continues, please contact us on:</p>            <p name="telNo" class="text-muted"></p> <!-- TEL NO. TO GO HERE -->        </span>        <span id="fourOfourTxt">            <p>The page you are looking for is not available. This may be because of one of the following reasons:</p>            <ul class="mb-3">                <li>The page may have been moved or removed</li>                <li>The page may have had its name changed</li>                <li>You may have typed the URL incorrectly</li>            </ul>        </span>        <a class="btn btn-primary col-12 col-lg-2 mt-3" href="/">Go back</a>    </div></div><div class="row footer">    <div class="col-lg-4">        &copy; 2020 Packnet Limited - All rights reserved    </div>    <div class="col text-right">        <i class="fas fa-phone fa-lg fa-rotate-90 mr-2"></i>        <span name="telNo"></span> <!-- TEL NO. TO ALSO GO HERE -->    </div></div></script>
查看完整描述

1 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

您需要循環該getElementsByName()函數的結果,因為它返回一個集合。請參閱下面的示例。正如評論中提到的,您不應該在 span 元素上使用 name 。最好使用類選擇器。相同的邏輯適用于getElementsByClassName()orquerySelectorAll()函數,因為這些函數也返回集合。

var telNo = '01234 567 8900 (8.30am till 5.30pm - Mon to Fri excluding bank holidays)';


document.getElementsByName('telNo').forEach((el) => {

  el.innerHTML = telNo;

});

<div class="row m-0 d-flex align-items-center p-5">

  <div class="col-12 col-lg-6 pr-lg-0">

    <h1>Oops, something went wrong</h1>

    <span id="otherErrorTxt" style="display: block">

            <p>This error has occurred whilst processing your request.</p>

            <p>If the error continues, please contact us on:</p>

            <p name="telNo" class="text-muted"></p> <!-- TEL NO. TO GO HERE -->

        </span>

    <span id="fourOfourTxt">

            <p>The page you are looking for is not available. This may be because of one of the following reasons:</p>

            <ul class="mb-3">

                <li>The page may have been moved or removed</li>

                <li>The page may have had its name changed</li>

                <li>You may have typed the URL incorrectly</li>

            </ul>

        </span>

    <a class="btn btn-primary col-12 col-lg-2 mt-3" href="/">Go back</a>

  </div>

</div>


<div class="row footer">

  <div class="col-lg-4">

    &copy; 2020 Packnet Limited - All rights reserved

  </div>

  <div class="col text-right">

    <i class="fas fa-phone fa-lg fa-rotate-90 mr-2"></i>

    <span name="telNo"></span>

    <!-- TEL NO. TO ALSO GO HERE -->

  </div>

</div>



查看完整回答
反對 回復 2024-01-03
  • 1 回答
  • 0 關注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號