简体   繁体   English

从所有锚标签中删除图像标签

[英]Remove image tags from inside all anchor tags

I have the following table header: 我有以下表格标题:

<table id="time" class="new_table" border="0" cellpadding="2">
    <thead>
        <tr>
            <th scope="col" rowspan="2" class="sortHead">
                <a href="#" id="unitSort">Unit</a>
            </th>
            <th colspan="2" id="startDateHead" class="sortHead">
                <a href="#" id="startDateSort">Start</a>
            </th>
            <th colspan="2" id="endDateHead" class="sortHead">
                <a href="#" id="endDateSort">End</a>
            </th>
            <th rowspan="2" class="sortHead">
                <a href="#" id="distanceSort">Distance (miles)</a>
            </th>
            <th rowspan="2" class="sortHead">
                <a href="#" id="locationSort">Location</a>
            </th>
            <th rowspan="2" class="sortHead">
                <a href="#" id="driverName">Driver Name</a>
            </th>
        </tr>

I am dynamically adding and removing img tags to the a tag when a user clicks a button. 当用户单击按钮时,我正在向标记中动态添加和删除img标记。 How can I remove all img tags within the a tags of this table when they are there? 如何删除此表的a标签中的所有img标签?

I have tried: 我努力了:

$('#time th a').each(function() { $(this).remove('img'); });

An example of the img element that is added to the a tag is as follows: 添加到a标签的img元素的示例如下:

<a href="#" id="driverName">Driver Name<img id="test" src="test.png" width="10"></a>

How can I remove all img tags within the a tags of this table when they are there. 当它们存在时,如何删除此表的a标签内的所有img标签。

Simply 只是

$('#time th a img').remove();

Demo 演示

Your attempt works if you flip the syntax a bit. 如果稍微翻转一下语法,您的尝试将起作用。 I can't explain why your version didn't work. 我无法解释为什么您的版本无法正常工作。 The docs indicate that it should. 该文档表明它应该。

$('#time th a').each(function() {
    $(this).find('img').remove();
});

Demo 演示

i modified your code little bit, i added .find("img") then use .remove() 我稍微修改了您的代码,添加了.find("img")然后使用.remove()

 $('#time th').each(function () { $(this).find("img").remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <table id="time" class="new_table" border="0" cellpadding="2"> <thead> <tr> <th scope="col" rowspan="2" class="sortHead"> <a href="#" id="driverName">Driver Name<img id="test" src="arrow.png" width="10" /></a> </th> <th scope="col" rowspan="2" class="sortHead"> <a href="#" id="A1">Driver Mobile<img id="Img1" src="arrow.png" width="10"></a> </th> </tr> </thead> </table> 

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM