最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the text from anchor tag using Jquery - Stack Overflow

programmeradmin4浏览0评论

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.....

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

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.....

alert($('#id1').text());
      alert($('#id1').html());
      alert($('#id1').val());
Share Improve this question edited Mar 6, 2013 at 9:46 Yevgeniy 2,6941 gold badge20 silver badges27 bronze badges asked Mar 6, 2013 at 9:44 mukundmukund 3275 gold badges10 silver badges22 bronze badges 3
  • Are you sure you have latest jquery.js imported in your workspace ? – The Dark Knight Commented Mar 6, 2013 at 9:48
  • text() and html() should work - what do they output in your example? Chances are it's a mistake at a more basic level, such as not importing jQuery as @TheDarkKnight suggests. Could also be you forgot to wrap it in $(function() { ... } ) to make sure the dom is loaded. – Armatus Commented Mar 6, 2013 at 9:50
  • possible duplicate of link and link2 – jeev Commented Mar 6, 2013 at 9:54
Add a ment  | 

3 Answers 3

Reset to default 4

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

Live Demo

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

Refer this link :

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

Get text from anchor tag

href:

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

Text:

$(function(){
$('div.res a').click(function(){
alert($(this).text());
 });
});
$(document).ready(function() 
        { 
            $("#id1").click(function() {
            alert($(this).text());
            return false;
            }); 
        } 
);
发布评论

评论列表(0)

  1. 暂无评论