【问题标题】:How to remove css property from last child element?如何从最后一个子元素中删除 css 属性?
【发布时间】:2013-11-18 21:16:46
【问题描述】:

这是动态代码,我不能将任何类或 id 分配给子元素,子元素的数量也不固定。我可以将类或 id 添加到我的表中,只有行无法访问代码对于 td。我需要从特定表的最后一个子元素中删除 margin-right 属性(该表是唯一的,我可以向该表添加类或 id) 这是我的代码

<table width="100%" cellspacing="0" cellpadding="12" border="0" class="category_list">
    <tr valign="top" height="99" class="category_list_row">
        <td class="categery-box category-right-margin"><a href="#"><img border="0" title="shop2.jpg" alt="shop2.jpg" src="#"></a>

            <div class="shop_content"><span class="shop_txt"><a onmouseout="this.className='lnk-HedOff'; return true;" onmouseover="this.className='lnk-HedOn'; return true;" ;="" class="lnk-HedOff" href="#">Laptops &amp; Notebooks</a>
    </span>
 <a onmouseout="this.className='lnk-HedOff'; return true;" onmouseover="this.className='lnk-HedOn'; return true;" ;="" class="lnk-HedOff" href="#"><input type="image" src="#"></a>
            </div>
        </td>
        <td class="categery-box category-right-margin"><a href="/s.nl/c.3747762/sc.7/category.59/.f"><img border="0" title="ipad.jpg" alt="ipad.jpg" src="/core/media/media.nl?id=130&amp;c=3747762&amp;h=d03cb241573f0b4c67c8"></a>

            <div class="shop_content"><span class="shop_txt"><a onmouseout="this.className='lnk-HedOff'; return true;" onmouseover="this.className='lnk-HedOn'; return true;" ;="" class="lnk-HedOff" href="#">iPADs &amp; Tablets</a>
    </span>
 <a onmouseout="this.className='lnk-HedOff'; return true;" onmouseover="this.className='lnk-HedOn'; return true;" ;="" class="lnk-HedOff" href="#"><input type="image" src="#"></a>
            </div>
        </td>
        <td class="categery-box category-right-margin"><a href="#"><img border="0" title="shop1.jpg" alt="shop1.jpg" src="#"></a>

            <div class="shop_content"><span class="shop_txt"><a onmouseout="this.className='lnk-HedOff'; return true;" onmouseover="this.className='lnk-HedOn'; return true;" ;="" class="lnk-HedOff" href="#">Mobile Phones</a>
    </span>
 <a onmouseout="this.className='lnk-HedOff'; return true;" onmouseover="this.className='lnk-HedOn'; return true;" ;="" class="lnk-HedOff" href="#"><input type="image" src="#"></a>
            </div>
        </td>
        <td class="categery-box category-right-margin"><a href="/s.nl/c.3747762/sc.7/category.49/.f"><img border="0" title="shop3.jpg" alt="shop3.jpg" src="#"></a>

            <div class="shop_content"><span class="shop_txt"><a onmouseout="this.className='lnk-HedOff'; return true;" onmouseover="this.className='lnk-HedOn'; return true;" ;="" class="lnk-HedOff" href="#">Cameras</a>
    </span>
 <a onmouseout="this.className='lnk-HedOff'; return true;" onmouseover="this.className='lnk-HedOn'; return true;" ;="" class="lnk-HedOff" href="/s.nl/c.3747762/sc.7/category.49/.f"><input type="image" src="#"></a>
            </div>
        </td>
    </tr>
</table>

如何使用 jquery 实现这一点?我想从最后一个 td 元素中删除 margin-right 属性。

【问题讨论】:

  • 你能知道数据什么时候更新吗?在这种情况下,您可以简单地在发生这种情况时绑定一个回调函数,然后使用 jQuery last child selector (api.jquery.com/last-child-selector)
  • 什么margin-right 属性?你的意思是.category-margin-rightCSS 属性?
  • 不,我不知道....有什么办法可以使用该表的类名并从最后一个 td 中删除 css 属性?
  • 我要删除的 css 属性

标签: javascript jquery html css-selectors


【解决方案1】:

您可以使用last 获取最后一个td,正如您所说的,您知道表'id'

$( "#mytable td:last" ).css("margin-right",""); 
//to change the css property
$( "#mytable td:last" ).removeClass('myclass');
//to remove last td class 

【讨论】:

  • 根据你的can we able to remove class of the last td element on the basis of table id?,我在上面更新了。
【解决方案2】:
$('table.category_list').find('td').eq(-1).css('margin-right', '');

这应该可以完成工作。

【讨论】:

    【解决方案3】:

    试试这个,

    $('table.category_list tr').each(function() {
          var lasttd=  $(this).find(':last-child');
          lasttd.css({margin-right:''});
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2014-12-09
      • 2014-11-28
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 2020-02-19
      相关资源
      最近更新 更多