javascript 如何使用 Jquery 从锚标记中获取文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15243790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:01:41  来源:igfitidea点击:

How to get the text from anchor tag using Jquery

javascriptjqueryjspdisplaytag

提问by mukund

The following is my anchor tag........

以下是我的锚标签…………

<display:setProperty name="paging.banner.full" value='<p><span id="hix-pagination"><span> <a id="id1" class="prev" href="{1}">&#9668;&#9668;</a>

And I tried the following using jquery but did not get any positive result.....

我使用 jquery 尝试了以下操作,但没有得到任何积极的结果.....

alert($('#id1').text());
      alert($('#id1').html());
      alert($('#id1').val());

回答by Adil

You have right selector, if you have jQuery included successfully then you may need document.readyalso check if you get html out of DOM

你有正确的选择器,如果你成功包含了 jQuery,那么你可能需要document.ready还要检查你是否从 DOM 中获取了 html

Live Demo

现场演示

$(function(){
   alert($('#id1').html());
});

回答by pavan

Refer this link :

请参阅此链接:

How to get anchor text/href on click using jQuery?

如何使用jQuery在点击时获取锚文本/href?

Get text from anchor tag

从锚标签获取文本

href:

href:

$(function(){
$('div.res a').click(function(){
alert($(this).attr('href'));
 });
});

Text:

文本:

$(function(){
$('div.res a').click(function(){
alert($(this).text());
 });
});

回答by Amrendra

$(document).ready(function() 
        { 
            $("#id1").click(function() {
            alert($(this).text());
            return false;
            }); 
        } 
);