簡體   English   中英

如何使用JavaScript更改沒有ID或類的錨鏈接的URL

[英]how to change the url of an anchor link that doesn't have an id or class using JavaScript

我無權訪問包含部分代碼的html文檔。 錨點鏈接不正確,用戶無法訪問信息和工具。 錨鏈接位於<h1>標記的內部,該標記位於table內部,該標記位於div內部。 現在div是唯一具有ID的東西,我無法將jQuery添加到此頁面包裝程序中,因為它已應用於1000多個頁面,所以我不想破壞任何內容。

HTML:

<div id="dashboard-home-content">
    <div id="bd-what-next" class="clearfix">
        <h2 id="msg_cat_what_next_question">What to do next?</h2>
        <div id="what-next-answer">Customize your Personal Page with a story about why you are raising funds for this cause.</div>
    </div>
    <!-- bd-what-next -->
    <div class="ux-block2" id="bd-org-html">
        <!-- TODO: Message Catalog this, Fix language -->
        <div id="organization_message">
            <table style="width: 100%;" border="0">
                <tbody>
                    <tr>
                        <td>
                            <h1><a href="change this url" target="_blank"><img src="../cco/images/content/pagebuilder/PC_FAQ_image.jpg" border="0" alt="Personal page image"><br>Participant Centre FAQ</a></h1>
                        </td>
                        <td>
                            <h1><a href="change this url" target="_blank"><img src="../cco/images/content/pagebuilder/PC_pic_image.jpg" border="0" alt="Personal page image"><br>Photo resize tool</a></h1>
                        </td>
                        <td>
                            <h1><a href="change this url" target="_blank"><img src="../cco/images/content/pagebuilder/PC_fundraising_image.jpg" border="0" alt="Personal page image"><br>Fundraising FAQ</a></h1>
                        </td>
                    </tr>
                </tbody>
            </table>
</div>

我知道用jQuery我可以尋找一個特定的元素來添加一個類,例如

$('td:contains("As part of your registration you will receive update emails and support from this program.")').addClass("td_regDetails");

但是我不知道如何用JavaScript做到這一點。

我在想是否有辦法向該錨鏈接添加類或ID,我可以做些類似的事情

var pcfaq = document.getElementById('wrongLinkOne');
pcfaq.href= "[[S8:PC_FAQ_updated.html]]"

(S8呈現html鏈接)。

謝謝您的幫助!

使用document.querySelectorAll()查找td內部的所有a標簽。 遍歷它們,檢查文本是否包含所需內容,然后更改href

document.querySelectorAll("td a").forEach(el => {
    if (a.textContent.includes("As part of your registration you will receive update emails and support from this program.")) {
        a.href = "[[S8:PC_FAQ_updated.html]]";
    }
});

暫無
暫無

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

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