(JQuery)'table.class'不会'不工作,我可以'我不知道为什么.它还选择

(JQuery) 'table.class' doesn't work and i can't figure out why. It selects tables with other classes too

本文关键字:我不知道 我可以 为什么 选择 不会 JQuery table class 工作      更新时间:2023-09-26

我的代码几乎是不言自明的。请看一下:

<html>
<body>
<!-- importing the latest jquery (1.6.4) here -->    
<table class="notThisOne" cellspacing="0" cellpadding="0" border="0" style="width: 100%; height: 20px;">
<colgroup>
<col>
</colgroup>
<tbody>
<tr class="not">
<td>
</td>
<td>
<b>Nicholas O'Reilly</b>
</td>
<td></td>
<td>nikoreilly@hotmail.com </td>
<td></td>
</tr>
<tr class="not">
<td>
</td>
<td>
<b>John Smith</b>
</td>
<td></td>
<td>jsmithjr@gmail.com </td>
<td></td>
</tr>
</tbody>
</table>

<table class="contactListRow" cellspacing="0" cellpadding="0" border="0" style="width: 100%; height: 20px;">
<colgroup>
<col>
</colgroup>
<tbody>
<tr class="contactListRow">
<td>
</td>
<td>
<b>Nicholas O'Reilly</b>
</td>
<td></td>
<td>nikoreilly@hotmail.com </td>
<td></td>
</tr>
<tr class="contactListRow">
<td>
</td>
<td>
<b>John Smith</b>
</td>
<td></td>
<td>jsmithjr@gmail.com </td>
<td></td>
</tr>
</tbody>
</table>
<br><br>
<script type="text/javascript">

$('table.contactListRow').each(function(i) {
    $name = "";
    $email = "";
    $('td').each(function(ii) {
        $col = ii%5;
        if( $col == 1){
            $name = $(this).text();
        }
        if( $col == 3){
            $email = $(this).text();
        }
        if( $col == 4){
            //$(this).html('<a href="javascript: openwindow(''https://mail.google.com/mail/?view=cm&fs=1&tf=1&to=&quot;'+$name+'&quot; &lt;'+$email+'&gt;'')">Email</a>');
            $(this).text(i+"_"+ii);
        }
    });
});
</script>
</body>
</html>

问题是,任何表都会被更改,即使类不是指定的类。

怎么了?

提前谢谢。

附言:我只需要它在Firefox 6+上运行(仅供个人使用(。

更改此行

$('td').each(function(ii) {

$(this).find('td').each(function(ii) {

您的问题就在这里:

$('td').each(function(ii) {

$('td')选择所有表格单元格,无论它们是否在您想要的表格中。因此,你并没有完全改变每个表,就像你(意外地(改变了每个表单元格一样。

请尝试使用$(this).find('td'),这样会将表单元格选择器限制在您感兴趣的表中。

   $('td').each(function(ii)

这不会改变每个td元素吗?

也许更像

xxxxx

无论如何,我还没有做太多的jScript,我的解决方案可能是不对的。

问题是您正在选择所有<td>元素,其中包含以下行:

$('td').each(function(ii) {

相反,您可以使用find在已经选择的表中查找,如下所示:

$(this).find('td').each(function(ii) {

(或者,要选择属于指定类的表的所有<td>元素,可以说$('table.contactListRow td').each(function(ii) {。(

相关文章: