How do you access the text in a <caption> tag?
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 comparison. 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
$('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.
精彩评论