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

javascript - JQUERY Get value from <span> on click - Stack Overflow

programmeradmin7浏览0评论

The problem is not that difficult, I just can't get my head around it (noob at Jquery). The problem all es down to when clicking a <span> getting it`s text and print it;

$('span').click(function(){
var t= ???;
    alert(t);
});

How can i get it's text??? NOTE: Each span does not have an id or class, any span clicked must output a message. Each span is generated dinamic via PHP and I need it's value.

The problem is not that difficult, I just can't get my head around it (noob at Jquery). The problem all es down to when clicking a <span> getting it`s text and print it;

$('span').click(function(){
var t= ???;
    alert(t);
});

How can i get it's text??? NOTE: Each span does not have an id or class, any span clicked must output a message. Each span is generated dinamic via PHP and I need it's value.

Share Improve this question edited Jul 12, 2011 at 8:13 jensgram 31.5k6 gold badges83 silver badges100 bronze badges asked Jul 12, 2011 at 8:11 ka_linka_lin 9,4326 gold badges38 silver badges58 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 10
$('span').click(function(){
    var t = $(this).text();
    alert(t);
});


See a demo at jsFiddle.

$('span').click(function(){
    var t= $(this).text();
    alert(t);
});

See .text().

use .text() (http://docs.jquery./Text())

$('span').click(function(){
        alert($(this).text());
    });

To get the span id without click

id = $("span.ClassName").attr('id');
发布评论

评论列表(0)

  1. 暂无评论