繁体   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