简体   繁体   English

动态更改<A>标记中的</a> HREF属性

[英]Dynamicaly changing HREF attribute in <A> tag

I am working on a website now. 我正在网站上工作。 The code is supposed to be very portable and run on different platforms. 该代码应该具有很高的可移植性,并且可以在不同的平台上运行。 I need to change the HREF attribute dynamically in my <a> tag. 我需要在<a>标记中动态更改HREF属性。
My code is looking like: 我的代码如下所示:

 function getValue(element) { var theValue = prompt('Please enter the value for the link: ' + element.getAttribute('HREF'), '0'); if (theValue == null) { return false; } var newLink = element.getAttribute('HREF') + ' ' + theValue; element.setAttribute('HREF', newLink); // test what I set: prompt(newLink); return true; } 
 <A HREF="https://www.w3schools.com" ONCLICK="return getValue(this)">click here</A> 

If the visitor clicks on "click me" link on my website, a prompt window will be shown. 如果访问者单击我网站上的“单击我”链接,将显示一个提示窗口。 If the user will enter the wished value, a website with the URL will open: https://www.w3schools.com theValue has to be opened. 如果用户输入希望的值,则将打开带有URL的网站: https://www.w3schools.com theValue必须打开“值”。

Right now, nothing happens. 现在,什么都没有发生。 Thank's a lot! 非常感谢!

I need to clarify couple thinks. 我需要澄清几个想法。 This example is very simplified. 这个例子非常简化。 My website is exporting links from engine for internal usage. 我的网站正在从引擎导出链接以供内部使用。 There is no security issue to fake links. 伪造链接没有安全性问题。 The website consists of 1.000 links as is www.w3schools.com. 该网站由1.000个链接组成,网址为www.w3schools.com。 It is no way to put some static IDs for <a> tags. 无法为<a>标签添加一些静态ID。 There is no way to create some forms. 无法创建某些表格。 This is not good solution to have links with matching edit boxes and buttons to post. 对于具有匹配的编辑框和要发布的按钮的链接,这不是一个好的解决方案。

The best solution could be to click on link, enter value in the prompt window and redirect URL as is mentioned. 最好的解决方案可能是单击链接,在提示窗口中输入值,然后如上所述重定向URL。 Thanks to Gil for review. 感谢吉尔的审查。 Now I see that the code works in IE, but it does not work in Mozilla browser. 现在,我看到该代码可在IE中运行,但无法在Mozilla浏览器中运行。

If you only want to redirect to the new URL you don't need to change the href of the element, just call window.open(newLink) and it will open a new tab with the specified link. 如果您只想重定向到新的URL,则无需更改元素的href,只需调用window.open(newLink) ,它将打开带有指定链接的新选项卡。 If you want to redirect on the same page then use window.location.href = newLink . 如果要在同一页面上重定向,请使用window.location.href = newLink

Note that you were creating an invalid URL with var newLink = element.getAttribute('HREF') + ' ' + theValue; 请注意,您使用var newLink = element.getAttribute('HREF') + ' ' + theValue;创建了无效的URL var newLink = element.getAttribute('HREF') + ' ' + theValue; you were adding the parameter with a space, you need a / . 您要在参数中添加一个空格,则需要一个/

 function getValue(element) { var theValue = prompt('Please enter the value for the link: ' + element.getAttribute('href'), '0'); if (theValue == null) { return false; } var newLink = element.getAttribute('href') + '/' + theValue; window.open(newLink); return true; } 
 <a href="https://www.w3schools.com" onclick="return getValue(this)">click here</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM