从所有锚标记内删除图像标记

时间:2016-06-14 14:07:14

标签: javascript jquery html

我有以下表格标题:

<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>

当用户点击按钮时,我正在动态添加和删除img标签。如何在该表的a标签中删除所有img标签?

我试过了:

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

添加到a标签的img元素的示例如下:

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

3 个答案:

答案 0 :(得分:5)

  

如何删除此表的a标签内的所有img标签   他们在那里。

简单地

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

<强> Demo

答案 1 :(得分:1)

如果稍微翻转语法,您的尝试是有效的。我无法解释为什么你的版本不起作用。 The docs表示它应该。

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

<强> Demo

答案 2 :(得分:0)

我稍微修改了你的代码,我添加.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>

希望这会对你有所帮助。