【问题标题】:Cannot set href and src of an element in jsjs中不能设置元素的href和src
【发布时间】:2021-02-19 23:06:39
【问题描述】:

我在 Tampermonkey 中有这个 javascript 代码:

var footerDiv = document.getElementsByClassName("copyright")[0];

window.addEventListener('load', function() {
    var button = document.createElement('button')
    footerDiv.append(button)
    button.src = 'https://img.shields.io/badge/%20-ComuBot--Tranlator-%23159818';
    button.href = "https://github.com/AnonHexo/public-scripts";
    button.id = 'srcButton';
})

但是当我运行代码时,按钮没有出现(显示为点“.”),并且没有 src 和 href 属性。

我尝试添加按钮的网站是https://comubot.cf

请帮助我。谢谢

【问题讨论】:

    标签: javascript html dom href src


    【解决方案1】:

    按钮元素没有srchref 属性。取而代之的是,您可以使用图像和锚元素来完成您的任务

    var footerDiv = document.getElementsByClassName("copyright")[0];
    
    window.addEventListener('load', function() {
        var a = document.createElement('a');
        a.id='srcButton';
        a.href = "https://github.com/AnonHexo/public-scripts";
        
        var img = document.createElement('img');
        img.src = 'https://img.shields.io/badge/%20-ComuBot--Tranlator-%23159818';
        a.append(img)
        footerDiv.append(a)
    })
    <div class="copyright"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2013-06-08
      • 2023-03-18
      • 2015-08-17
      • 2012-11-07
      • 2017-10-29
      • 2021-10-24
      • 2018-07-03
      相关资源
      最近更新 更多