简体   繁体   English

在点击href网址之前对其进行编辑?

[英]Edit a href tag url before it's clicked on?

I have a url that looks something like this: 我有一个看起来像这样的网址:

https://url.com/abc

which is as expected. 正如预期的那样。 When the user clicks on this however, it redirects to the wrong link because the link actually has underscores instead of spaces in its url. 但是,当用户单击此链接时,它会重定向到错误的链接,因为该链接实际上在其URL中带有下划线而不是空格。 is there any way to edit this url so that right before the url is clicked, the spaces turn into underscores? 有什么方法可以编辑此URL,以便在单击URL之前,空格变成下划线? The url would look like so: 网址看起来像这样:

https://url.com/a_b_c when the user clicks it instead of what was above. 当用户单击它而不是上面的内容时,请单击https://url.com/a_b_c Below is my attempt: 以下是我的尝试:

<a target="_blank" style="font-size: 9px" href="https://url.com/{{label}}">[Details]</a>

where label is equal to abc . 其中label等于abc I tried the following: 我尝试了以下方法:

<a target="_blank" style="font-size: 9px" href="https://url.com/{{label.replace(" ", "_")}}">[Details]</a>

so that abc would turn into a_b_c but that didn't seem to work because the curly braces can't actually execute the replace function. 这样abc会变成a_b_c但似乎不起作用,因为花括号实际上无法执行replace函数。 Any help would be appreciated. 任何帮助,将不胜感激。 If possible, I would also want to append some text to the end of this newly replaced label so it would look like: a_b_c_new_text Thanks! 如果可能的话,我还想在此新替换的标签的末尾附加一些文本,使其看起来像: a_b_c_new_text谢谢!

For some reason I was having difficulty getting the global regular expression replace .replace(/ /g, '_') working within the curly braces. 出于某种原因,我有困难得到全球正则表达式替换.replace(/ /g, '_')大括号内工作。 Instead, I opted for a solution from this SO answer. 相反,我选择了从溶液中这个 SO答案。

<a ng-href="http://url.com/{{label.split(' ').join('_')}}">Click Me</a>

Here's the Codepen. 这是Codepen。

Keep in mind that ng-href is better than href because it will not throw a 404 before the curly braces are evaluated. 请记住, ng-hrefhref更好,因为在评估花括号之前它不会抛出404。

Try this: JS Fiddle 试试这个: JS小提琴

 var $link = document.getElementById('test'), $href = test.href; test.addEventListener('mouseover', function(){ $href = $href.replace(/%20/g, '_'); console.log($href); this.href = $href; }); 
 <a id="test" href="https://url.com/abc">Link Data</a> 

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

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