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

javascript - How do you access the text in a <caption> tag? - Stack Overflow

programmeradmin8浏览0评论

I have my HTML like this:

<table>
<caption class="my_caption">Table 1.1: TABLE CAPTION</caption> 

<tr>...</tr>
<tr>...</tr>
...

I need to get the caption text so I can make some string parison. I've tried doing .val(), .text(), .html() and .value but none of them work.

Thanks for your help.

EDIT: I actually have a few of those captions. Sorry, I should've mentioned this earlier.

<div>
<table>
<caption class="my_caption">Table 1.1</caption> 

<tr>...</tr>
<tr>...</tr>
...
</table>
<table>
<caption class="my_caption">Table 1.2</caption> 

<tr>...</tr>
<tr>...</tr>
...
</table> </div>

So I have a for -loop that goes through all the captions:

        var cap_tables = $("caption.my_caption");
        for (var i=0;i<cap_tables.length;i++) {
            alert(cap_tables[i].text());
            //i've tried .text(), .html(), .val(), .value to get the caption text 
        }

I will try again will all your suggestions and get back to you guys. Thank you very much!!!

I am using Firefox version 3.5.3

I have my HTML like this:

<table>
<caption class="my_caption">Table 1.1: TABLE CAPTION</caption> 

<tr>...</tr>
<tr>...</tr>
...

I need to get the caption text so I can make some string parison. I've tried doing .val(), .text(), .html() and .value but none of them work.

Thanks for your help.

EDIT: I actually have a few of those captions. Sorry, I should've mentioned this earlier.

<div>
<table>
<caption class="my_caption">Table 1.1</caption> 

<tr>...</tr>
<tr>...</tr>
...
</table>
<table>
<caption class="my_caption">Table 1.2</caption> 

<tr>...</tr>
<tr>...</tr>
...
</table> </div>

So I have a for -loop that goes through all the captions:

        var cap_tables = $("caption.my_caption");
        for (var i=0;i<cap_tables.length;i++) {
            alert(cap_tables[i].text());
            //i've tried .text(), .html(), .val(), .value to get the caption text 
        }

I will try again will all your suggestions and get back to you guys. Thank you very much!!!

I am using Firefox version 3.5.3

Share Improve this question edited Dec 21, 2009 at 18:34 Corrine asked Dec 21, 2009 at 18:19 CorrineCorrine 2032 gold badges6 silver badges11 bronze badges 2
  • 1 .text() or .html() should work. Lets see your selector. – Crescent Fresh Commented Dec 21, 2009 at 18:25
  • 1 Another good piece of info would be which browser and version you are using as it seems from all our answers below, it should work. – cjstehno Commented Dec 21, 2009 at 18:30
Add a ment  | 

4 Answers 4

Reset to default 7
$('caption').text();

or

$('.my_caption').text();

You should try the built in jquery iterator to go over each element rather than that loop you posted.

        $("caption.my_caption").each(function(i,val){
                    alert($(this).text());
                    });

or, if you have a "table" object, your code might look like:

var myTable = $('table');
var myCaptionText = myTable.find('caption').text();

This works in the latest Firefox and IE:

jQuery(function(){
    alert( $('caption').html() );
});

Not sure why its not working for you.

发布评论

评论列表(0)

  1. 暂无评论