【问题标题】:Remove image tags from inside all anchor tags从所有锚标签中删除图像标签
【发布时间】:2016-06-14 22:44:24
【问题描述】:

我有以下表格标题:

<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 标签。我如何删除该表的 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>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

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

    简单

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

    Demo

    【讨论】:

      【解决方案2】:

      如果你稍微改变一下语法,你的尝试就会奏效。我无法解释为什么您的版本不起作用。 The docs 表示应该这样做。

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

      Demo

      【讨论】:

        【解决方案3】:

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

        希望这会对你有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-11
          • 2013-12-05
          • 1970-01-01
          • 2016-01-23
          • 2015-08-21
          • 2013-01-30
          • 1970-01-01
          • 2013-05-26
          相关资源
          最近更新 更多