我在页面上具有以下动态生成的表以及该页面上的其他表。我需要删除没有id的表。我可以想到将表定位为要删除的两种方法之一,因为这些方法永远不会改变。

通过class =“ matching_results_text”或
通过文字“你在这里”

只是不知道该怎么做。

我试过$(“ table”)。remove(“:contains('您在这里:')”);但这行不通。

<table width="100%" border="0" cellspacing="0" cellpadding="5">
          <tr>
            <td>
                <b>You are here: <a href="http://www.test.com">Home</a> &gt; <a href="http://www.test.com/Ignition-Controls-s/30.htm" title="Ignition Controls">Ignition Controls</a></b>
<div class="matching_results_text">We found 10 results matching your criteria.</div>

 </td>
          </tr>
        </table>

最佳答案

编辑:请注意,如果div.matching_results_texta[title=Ignition Controls]有多个元素,它将不起作用,因为您将匹配所有这些元素。

如果是这种情况,请尝试我提供的最后一个解决方案,或者发布更多的HTML结构。可能会有祖先元素会有所帮助。



jQuery的.closest()方法返回一个结果,其中包含与选择器匹配的最接近的祖先。

    // Starting point is the class name
$('table div.matching_results_text').closest('table').remove();


要么

    // Starting point is the <a> element with the title Ignition Controls
$('table a[title=Ignition Controls]').closest('table').remove();


只是为了好玩,我将再抛出一个使用:contains().closest()的选项。

    // Starting point is the <b> element that contains "You are here"
$('table b:contains(You are here)').closest('table').remove();


http://api.jquery.com/closest/

如果您知道它是(例如)页面上的第3个表,则可以使用索引引用。

     // Get the third table on the page
$('table:eq(2)').remove();


http://api.jquery.com/eq-selector/

07-24 09:38
查看更多