【问题标题】:How can I change a href attribute with javascript?如何使用 javascript 更改 href 属性?
【发布时间】:2016-04-10 10:21:04
【问题描述】:

大家好,我的页面中有一个read more 链接,但我想通过 javascrpt 更改它的链接。我有这个链接http://london/wa/al/Lists/Announcements/DispForm.aspx?ID=4,但这个链接不再起作用,我也无法再访问 html。所以需要改成下面的http://london/wa/meridianexpress/Lists/Announcements/DispForm.aspx?ID=4

我怎样才能做到这一点?

【问题讨论】:

  • 你能说得更具体些吗?
  • 什么意思,你没有访问html的权限??
  • 我无法再更改 HTML

标签: javascript c# html css asp.net


【解决方案1】:

也许你可以试试这个:

window.onload = function ()
        {
            var allLinksOnPage = document.getElementsByTagName("a");
            for (var i = 0; i < allLinksOnPage.length; i++) {
                if (allLinksOnPage[i].href == 'http://london/wa/al/Lists/Announcements/DispForm.aspx?ID=4')
                    allLinksOnPage[i].href = 'http://london/wa/meridianexpress/Lists/Announcements/DispForm.aspx?ID=4';
            }           
        }

希望这会有所帮助...

【讨论】:

    【解决方案2】:

    您可以在任何现有的 onload 函数中添加以下行,或在您有权访问且已包含在 html 中的任何脚本文件中创建如下行

    window.onload = function(){
      document.getElementById("more").href="xyz.php";
    }
    

    【讨论】:

    • "more" 这是html的类或者它是什么。我是 JavaScript 和 html 的新手,很抱歉我的愚蠢问题
    【解决方案3】:

    试试这个: document.getElementById("yourhtmllikid").setAttribute("href", "http:...");

    【讨论】:

    • 你是说试试这个:document.getElementByID("http://london/wa/al/Lists/Announcements/DispForm.aspx?ID").setAttribute("href","http://london/wa/meridianexpress/Lists/Announcements/DispForm.aspx?ID")我说的对吗?
    • 没有。您必须在您的 HTML 标记处提供一个 id。例如 然后用 JavaScript 和我的建议你可以解决你的问题!
    • 我必须使用"Read more"
    • 我不明白,请给我更多信息
    猜你喜欢
    • 2013-06-13
    • 1970-01-01
    • 2011-06-21
    • 2015-07-16
    • 2021-05-01
    • 2014-07-21
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多