2016-07-31 83 views
0

我想選擇下面的HTML表格的第5個元素:<table border="0" style="width: 577px; height: 224px;">,然後提醒innerhtml以確保我選擇了正確的東西。jQuery Selector(children,eq和innerhtml)

我的jQuery代碼:alert($('#content-text').children().eq(5).innerhtml);

HTML塊...

<div id="content-text"> 
<p><span style="font-size: large;"><span style="font-family: comic sans ms,sans-serif;">Title</span></span></p> 
<p>&nbsp;</p> 
<p><span style="font-size: large;"><span style="font-family: comic sans ms,sans-serif;">Directions</span></span></p> 
<table border="0" style="width: 577px; height: 207px;"> 
    <tbody> 
     <tr> 
      <td valign="middle">link 1</td> 
      <td valign="middle">link 2</td> 
     </tr> 
    </tbody> 
</table> 
<table border="0" style="width: 577px; height: 224px;"> 
    <tbody> 
     <tr> 
      <td style="text-align: center;"><span style="font-size: medium;">more directions</span> 
       <p><span style="font-family: tahoma,arial,helvetica,sans-serif;">info</span></p> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Practice probs. 
      </td> 
     </tr> 
    </tbody> 
</table> 
<table width="610" height="338" cellpadding="0" border="0" style="width: 607px; height: 338px;"> 
    <tbody> 
     <tr> 
      <td> 
       Assignment problems listed out 
      </td> 
      <td style="padding-left: 40px;"> 
       Image 
      </td> 
     </tr> 
     <tr> 
      <td> 
       info. about correcting problems 
      </td> 
     </tr> 
    </tbody> 
</table> 

任何想法,我的jQuery選擇似乎爲何不奏效?

+1

'innerHTML'是JavaScript的一部分而'的html ()'應該使用在jQuery – dinesh

+1

'$('#content-text')。children()。eq(5).html()'會打印表格的DOM元素,但如果您只需要表格的文本內容,請嘗試'$( '#內容的文本')。子女()。方程(5)的.text()' – dinesh

回答

2

innerHTML是javascript屬性。改爲使用html(),如下所示。

alert($('#content-text').children().eq(4).html()); 

NB:不eq(5)eq(4)將選擇<table border="0" style="width: 577px; height: 224px;">

0

一種不同的方法是:

$('#content-text').children('[style="width: 577px; height: 224px;"]')[0].outerHTML 

$(function() { 
 
    console.log($('#content-text').children('[style="width: 577px; height: 224px;"]')[0].outerHTML); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="content-text"> 
 
    <p><span style="font-size: large;"><span style="font-family: comic sans ms,sans-serif;">Title</span></span></p> 
 

 
    <p>&nbsp;</p> 
 

 
    <p><span style="font-size: large;"><span style="font-family: comic sans ms,sans-serif;">Directions</span></span></p> 
 
    <table border="0" style="width: 577px; height: 207px;"> 
 
     <tbody> 
 
     <tr> 
 
      <td valign="middle">link 1</td> 
 
      <td valign="middle">link 2</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
    <table border="0" style="width: 577px; height: 224px;"> 
 
     <tbody> 
 
     <tr> 
 
      <td style="text-align: center;"><span style="font-size: medium;">more directions</span> 
 

 
       <p><span style="font-family: tahoma,arial,helvetica,sans-serif;">info</span></p> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       Practice probs. 
 
      </td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
    <table width="610" height="338" cellpadding="0" border="0" style="width: 607px; height: 338px;"> 
 
     <tbody> 
 
     <tr> 
 
      <td> 
 
       Assignment problems listed out 
 
      </td> 
 
      <td style="padding-left: 40px;"> 
 
       Image 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       info. about correcting problems 
 
      </td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
</div>