在JavaScript中点击链接时获取链接的文本。

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

Get Text of Link when Clicked in JavaScript

问题

以下是一个在我接手的项目中的网址示例。

单击链接时,我需要获取文本“单击链接”。

我已经在网上搜索过,所有的建议都不起作用。

任何帮助都将不胜感激。

<a href="site/page" onclick="loadContent('https://site/page.com');">
   <span class="navitem">单击链接</span>
</a>

在一个 span 标签中的文本。

我尝试过:

$('.navitem').on('click', 'a', function() {
    var text = $(this).text();
});

但控制台显示“$未定义”。

我还尝试过:

$('a').live('click', function() {
    window.location.href = $(this).closest('span').text();
});
英文:

Below is a sample of a URL that is in a site (project that I inherited).

When clicking on the link, I need to get the text "Click Link".

I have searched the web and all suggestions do not work.

Any help is appreciated.

 &lt;a href=&quot;site/page&quot; onclick=&quot;loadContent(&#39;https://site/page.com&#39;);&quot;&gt;
   &lt;span class=&quot;navitem&quot;&gt;Click Link&lt;/span&gt;
 &lt;/a&gt;

The text in a span tag.

I've tried

$(&#39;. navitem&#39;).on(&#39;click&#39;, &#39;a&#39;, function() {
    var text = $(this).text();
});

But Console log says $ not defined.
I also tried

$(&#39;a&#39;).live(&#39;click&#39;, function() {
    window.location.href = $(this).closest(&#39;span&#39;).text();
});

答案1

得分: 1

document.querySelector('a').addEventListener('click', function() {
    var text = this.querySelector('span').textContent
    
    // Do anything with text
    console.log(text)
})
英文:
document.querySelector(&#39;a&#39;).addEventListener(&#39;click&#39;, function() {
    var text = this.querySelector(&#39;span&#39;).textContent
    
    // Do anything with text
    console.log(text)
})

答案2

得分: 1

你的第一个代码片段包含两个错误。更正如下:

$('.navitem').on('click', function () {
    var text = $(this).text();
    window.location.href = text; //?? 无论你想要做什么
});

.navitem之间有一个不必要的空格。另一个错误在于on方法。我移除了传递的参数a

根据你上一个评论 - 在你的问题下方 - 你需要将其包装在:

$(document).ready(function() {
    $('.navitem').on('click', function () {
        var text = $(this).text();
        window.location.href = text; //?? 无论你想要做什么
    }); 
});

另一个问题是你的HTML代码片段。所呈现的代码没有意义:

<a href="site/page" onclick="loadContent('https://site/page.com');">
    <span>...</span>
</a>

你的a标签重叠了span标签,而href属性重叠了onclick属性,所以href的值会首先被执行。你添加的span元素的代码不会被执行。

英文:

Your first code snippet contains two misstakes. Change this one by

$(&#39;.navitem&#39;).on(&#39;click&#39;, function ()
  {
      var text = $(this).text();
       window.location.href = text; //?? What eveer you want to do that 
  });

There was an unnessacry space between '.' and 'navitem'. Another one was in the on method. I removed the passed parameter 'a'.

Based on your last comment - underneath your question- you have to wrap this into

$(document).ready(function()
{
$(&#39;.navitem&#39;).on(&#39;click&#39;, function ()
  {
      var text = $(this).text();
       window.location.href = text; //?? What eveer you want to do that 
  }); 
});

Another point is your html code snippet. The presented code makes not sense:

&lt;a href=&quot;site/page&quot; onclick=&quot;loadContent(&#39;https://site/page.com&#39;);&quot;&gt;
&lt;span&gt;...&lt;/span&gt;
&lt;/a&gt;

Your a tag overlaps you span tag and the href attribute overlaps the onclick attribute, so the value of href will be executed first. Your added code for span element won't be executed.

答案3

得分: 0

在你的以下jQuery代码中:

$('.navitem').on('click', 'a', function() {
    var text = $(this).text();
});

你在navitema上绑定了事件,但在你的情况下是不正确的。应该像下面这样修改成 $('a').on('click', '.navitem')。我已经在下面的代码片段中更新了这个点击事件。

$('.navitem').off("click").on('click', function(e) {
  e.preventDefault();
  var text = $(this).text();
  console.log(text);
});

类似地,当在你的第二段代码中使用live方法时,可能会出现以下错误:

Uncaught TypeError: $(...).live 不是一个函数

这是因为live API从jQuery 1.9版本开始已经被弃用并移除

英文:

In your following jQuery code:

$(&#39;.navitem&#39;).on(&#39;click&#39;, &#39;a&#39;, function() {
    var text = $(this).text();
});

you have deleget event on a of navitem which in your case is incorrect. It need to be something like $(&#39;a&#39;).on(&#39;click&#39;, &#39;.navitem&#39;). I have update this click event in following snippet.

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

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

$(&#39;.navitem&#39;).off(&quot;click&quot;).on(&#39;click&#39;, function(e) {
  e.preventDefault();
  var text = $(this).text();
  console.log(text);
});

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;a href=&quot;site/page&quot; onclick=&quot;loadContent(&#39;https://site/page.com&#39;);&quot;&gt;
  &lt;span class=&quot;navitem&quot;&gt;Click Link&lt;/span&gt;
&lt;/a&gt;

<!-- end snippet -->

Similarly, when using live method in your, second code you might get

> Uncaught TypeError: $(...).live is not a function

this is because the live API is deprecated has been removed since jQuery 1.9

huangapple
  • 本文由 发表于 2023年7月27日 18:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778730.html
匿名

发表评论

匿名网友

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

确定