Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
HTML:
<table> <tr> <td> <a href="#">link</a> </td> </tr> </table>
我想:在“a”中查找“链接”并剥离除/直到“表”之外的所有父标签。我知道这不是理想的方法,但有时您无法修改标记,因此了解 jquery hack 会很棒。
var a = $("a:contains('link')"); var table = a.closest('table'); a.insertBefore( table ); // Edit: See comments below table.contents().remove(); table.append( a );
编辑:调用remove()还将删除可能附加到锚元素的任何事件处理程序。要解决这个问题,请在删除其内容之前将锚移到表格之外。
remove()