通过查找 id- javascript 更改 href

Change href by finding id- javascript

我是 JS 的新手,也是一名 Swift 开发人员。我有一个按钮 属性:

<a href="http://phonefiveme.com/nocontact" id="yui_3_17_2_1_1490136681073_127" style="cursor: url(&quot;chrome-extension://ledmjlnkdlappilhaaihfhanlpdjjalm/rockhand.png&quot;), auto;">

我需要做的是将该 href 编辑为存储在变量中的 link。这是我拥有的:

    var currentLocation = window.location;
    var stringURL = String(currentLocation);
    var shortened = stringURL.substring(32);
    var myElement = document.getElementById("yui_3_17_2_1_1490136681073_127");

我只需要将“http://phonefiveme.com/nocontact”替换为shortened

如有任何帮助,我们将不胜感激!非常感谢。 干杯, 西奥

简单:

var currentLocation = window.location;
var stringURL = String(currentLocation);
var shortened = stringURL.substring(32);
var myElement = document.getElementById("yui_3_17_2_1_1490136681073_127");
myElement.href = myElement.href.replace("http://phonefiveme.com/nocontact", shortened);

您可以使用 href JavaScript 属性.

只需添加到您的 JavaScript:

myElement.href = shortened;

请参阅 similar 问题以获得更多参考。

您可以访问 html 元素的属性,只需使用

myElement.href=shortened;

做这个把戏