简体   繁体   English

如果单击,则将href文本添加到链接

[英]Add href text to link if clicked

I have a page with several links. 我有一个包含多个链接的页面。 Some links point to the same exact URL. 某些链接指向相同的URL。 I want to add the text (the linkable href text) to the link that is clicked (or hovered?) when one of these duplicate links are clicked. 我想在单击其中一个重复链接时将文本(可链接的href文本)添加到单击(或悬停?)的链接。

So far I can only seem to add the first link's text but not the one I click. 到目前为止,我似乎只能添加第一个链接的文本而不是我点击的文本。

I have tried a few things but I am still learning JavaScript (I envy you all) and am afraid I keep messing it up the harder I try. 我尝试过一些东西,但我还在学习JavaScript(我很羡慕你们),我担心我会越努力搞砸它。 I know I will likely have to use hover or somehow change getElementsByTagName("a")[0]... Any help would be greatly appreciated! 我知道我可能不得不使用悬停或以某种方式更改getElementsByTagName("a")[0]...任何帮助将不胜感激!

<p>
<a href="http://www.1.com" target="_blank">dog</a>
<a href="http://www.2.com" target="_blank">cat</a>
<a href="http://www.3.com" target="_blank">bird</a>
<a href="http://www.3.com" target="_blank">frog</a>
</p>

Javascript: 使用Javascript:

$var anchors = document.querySelectorAll('a[href$="http://www.3.com"]');
Array.prototype.forEach.call(anchors, function (element, index) {
    element.href = document.getElementsByTagName("a")[0].innerHTML;
});

Example When I click on the link "dog", I want to open a new browser tab/window with the URL of " http://www.1.com " 示例当我单击链接“dog”时,我想打开一个新的浏览器选项卡/窗口,其URL为“ http://www.1.com

When I click on the link "cat", I want to open a new browser tab/window with the URL of " http://www.2.com " 当我点击链接“cat”时,我想打开一个新的浏览器标签/窗口,其URL为“ http://www.2.com

When I click on the link "bird", I want to open a new browser tab/window with the URL of "bird" 当我点击“鸟”链接时,我想打开一个新的浏览器标签/窗口,其URL为“bird”

When I click on the link "frog", I want to open a new browser tab/window with the URL of "frog" 当我点击“青蛙”链接时,我想打开一个新的浏览器标签/窗口,其URL为“青蛙”

So any link that has " http://www.3.com " as the href will ignore the " http://www.3.com " and instead open a new browser tab/window with the text that was just clicked. 因此,任何具有“ http://www.3.com ”作为href的链接都将忽略“ http://www.3.com ”,而是打开一个新的浏览器选项卡/窗口,其中包含刚刚单击的文本。

Also, I am unable to add IDs, Class, script, etc. to the links/HTML so I have to figure out how to do all of this as JS referencing the href and not inside of the HTML. 此外,我无法在链接/ HTML中添加ID,类,脚本等,因此我必须弄清楚如何将所有这些作为JS引用href而不是HTML内部。

Try like this: 试试这样:

<a href="" onClick="javascript:this.innerHTML = 'ChangedClickMe'">ClickMe</a>

JSFIDDLE DEMO JSFIDDLE DEMO

Not sure if this is exactly what you're asking for but THIS DEMO is an example where I take the href and append it to the text. 不确定这是否正是您所要求的,但本演示是一个示例,我将href并将其附加到文本中。

html HTML

<a href='#' onclick='change(this)'>link one</a>
<a href='#' onclick='change(this)'>link two</a>
<a href='#' onclick='change(this)'>link three</a>
<a href='#' onclick='change(this)'>link four</a>

css CSS

a {
    float: left;
    clear: both;
}

javascript JavaScript的

function change(variable) {
    variable.innerHTML = variable.innerHTML + variable.href;
}

A colleague of mine found the solution. 我的一位同事找到了解决方案。 Thank's everyone for your help. 感谢大家的帮助。

Option 1: 选项1:

Replace part of my original JS: 替换我原来的JS的一部分:

" document.getElementsByTagName("a")[0].innerHTML " document.getElementsByTagName(”a“)[0] .innerHTML

with: 有:

" window.location " window.location

Or 要么

Option 2: jsfiddle 选项2: jsfiddle

Replace all of my original JS: 替换我原来的所有JS:

$var anchors = document.querySelectorAll('a[href$="http://www.3.com"]');
Array.prototype.forEach.call(anchors, function (element, index) {
element.href = document.getElementsByTagName("a")[0].innerHTML;
});

With: 附:

var anchors = $('a[href$="http://www.3.com"]');
$.each(anchors, function (index, element) {
$(element).attr('href',$(element).text());
});

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

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