如何使用JavaScript获取标签中的超链接?

huangapple go评论74阅读模式
英文:

How to get the hyperlink in a tag using javascript?

问题

这是一个HTML和JavaScript的代码示例,你想知道如何在a标签中获取链接的一般方法。

这是一个获取链接的一般方法的示例代码:

document.addEventListener('mousedown', function(event) {
  let elem = event.target;
  if (elem.tagName === 'A') {
    let link = elem.getAttribute('href');
    if (event.button === 2) {
      alert(link);
    }
  }
});

这个代码会在鼠标右键点击链接时弹出链接地址。

英文:

Here I have an html with javascript:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.addEventListener(&#39;mousedown&#39;, function(event) {
  let elem = event.target;
  let jsonObject = {
    Key: &#39;mousedown&#39;,
    Value: event.button
  };
  if (event.button == 2) {
    console.log(elem.outerHTML);
  }
});

<!-- language: lang-html -->

&lt;span style=&quot;font-size:xx-large&quot;&gt;
    &lt;a href=&quot;https://microsoft.com&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; shape=&quot;rect&quot; style=&quot;color:rgb(17,85,204)&quot;&gt;
        &lt;i&gt;
            &lt;font color=&quot;#ff0000&quot;&gt;Microsoft&lt;/font&gt;
        &lt;/i&gt;
    &lt;/a&gt;
&lt;/span&gt;
&lt;br/&gt;
&lt;a href=&quot;https://google.com&quot;&gt;Google&lt;/a&gt;
&lt;div&gt;

<!-- end snippet -->

When I right click the link, it should alert me the hyperlink inside.

The Google works fine. But when right click Microsoft, it always returns the font tag.

I also tried this, still not work:

document.addEventListener(&#39;mousedown&#39;, function (event)
                              {
                                let elem = event.target;
                                let name = elem.tagName;
                                let jsonObject =
                                {
                                    Key: &#39;mousemove&#39;,
                                    Value: name == &#39;A&#39; ? elem.outerHTML : &#39; &#39;
                                };

                                if(event.button == 2 &amp;&amp; name == &#39;A&#39;) 
                                  {alert(elem.outerHTML);}
                              });

Is there a general method for getting the links in the a tags?

答案1

得分: 1

你可以遍历父节点,直到找到锚点,就像这样:

document.addEventListener('mousedown', event => {
  let elem = event.target;
  while(elem && elem.tagName!=='A') elem = elem.parentElement; 
  if(elem) {
    let jsonObject = 
    {
        Key: 'mousedown',
        Value: event.button
    };
    if(event.button == 2) {alert(elem.href);}
  }
});
<span style="font-size:xx-large">
    <a href="https://microsoft.com" target="_blank" rel="noreferrer noopener" shape="rect" style="color:rgb(17,85,204)">
        <i>
            <font color="#ff0000">Microsoft</font>
        </i>
    </a>
</span>
<br/>
<a href="https://google.com">Google</a>
<div>
英文:

You can traverse the parent nodes until you find the anchor, like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.addEventListener(&#39;mousedown&#39;, event =&gt; {
  let elem = event.target;
  while(elem &amp;&amp; elem.tagName!==&#39;A&#39;) elem = elem.parentElement; 
  if(elem) {
    let jsonObject = 
    {
        Key: &#39;mousedown&#39;,
        Value: event.button
    };
    if(event.button == 2) {alert(elem.href);}
  }
});

<!-- language: lang-html -->

&lt;span style=&quot;font-size:xx-large&quot;&gt;
    &lt;a href=&quot;https://microsoft.com&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; shape=&quot;rect&quot; style=&quot;color:rgb(17,85,204)&quot;&gt;
        &lt;i&gt;
            &lt;font color=&quot;#ff0000&quot;&gt;Microsoft&lt;/font&gt;
        &lt;/i&gt;
    &lt;/a&gt;
&lt;/span&gt;
&lt;br/&gt;
&lt;a href=&quot;https://google.com&quot;&gt;Google&lt;/a&gt;
&lt;div&gt;

<!-- end snippet -->

答案2

得分: 1

以下是您要翻译的内容:

"Closest should do it"

英文:

Closest should do it

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.addEventListener(&#39;mousedown&#39;, function(event) {
  let elem = event.target.closest(&quot;a&quot;);
  if (!elem) return; // not a link
  let jsonObject = {
    Key: &#39;mousedown&#39;,
    Value: event.button
  };
  if (event.button == 2) {
    console.log(elem.textContent.trim(),&quot;:&quot;,elem.href,&quot;\n&quot;,elem.outerHTML);
  }
});

<!-- language: lang-html -->

&lt;span style=&quot;font-size:xx-large&quot;&gt;
    &lt;a href=&quot;https://microsoft.com&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; shape=&quot;rect&quot; style=&quot;color:rgb(17,85,204)&quot;&gt;
        &lt;i&gt;
            &lt;font color=&quot;#ff0000&quot;&gt;Microsoft&lt;/font&gt;
        &lt;/i&gt;
    &lt;/a&gt;
&lt;/span&gt;
&lt;br/&gt;
&lt;a href=&quot;https://google.com&quot;&gt;Google&lt;/a&gt;
&lt;div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月9日 12:13:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定