通过查找id-javascript更改href

通过查找id-javascript更改href,javascript,jquery,href,getelementbyid,Javascript,Jquery,Href,Getelementbyid,我是JS的新手,也是一名敏捷的开发人员。我有一个具有此属性的按钮: <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;"> 我只需要将“”替换为缩短的 任何

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

<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;">
我只需要将“”替换为缩短的

任何帮助都将不胜感激!非常感谢。 干杯 Theo

简单:

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);
简单:

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);
您可以使用JavaScript属性来实现这一点

只需在JavaScript中添加以下内容:

myElement.href = shortened;
有关更多参考,请参阅问题。

您可以使用JavaScript属性执行此操作

只需在JavaScript中添加以下内容:

myElement.href = shortened;

更多参考请参见问题。

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

myElement.href=shortened;

要执行此操作,您可以访问html元素的属性,只需使用

myElement.href=shortened;

做这个把戏

你上面的问题也行吗?如果它们都是正确的,我会把它们都标记为正确的do@TheoStrauss两者将以同样的方式工作。然而,另一个答案是替换字符串。字符串替换方法依赖于href最初保留为“”,否则它将无法正确替换字符串。您上面的问题也适用吗?如果它们都是正确的,我会把它们都标记为正确的do@TheoStrauss两者将以同样的方式工作。然而,另一个答案是替换字符串。字符串替换方法依赖于href最初保留为“”,否则将无法正确替换字符串。