使用jQuery循环遍历表中的所有图像

问题描述 投票:0回答:2

我在jQuery中不是那么好,或者根本不是很好,但是我需要创建一个jQuery / Javascript函数来搜索特定表中的所有图像。所以标准是表必须有一个属性'summary',它必须等于'forum'(summary = forum)

<table width="100%" class="ms-disc"  dir="none" border="0"
 cellSpacing="0" cellPadding="1" summary="Forum">

所以如果这是表格,那么我需要检查td是否有类“特定类”<td class="ms-disc-bordered">。有可能在那个td可以是另一个表,只有在那个表中有一个td ...但这并不重要,只是知道它是嵌套的,并且可以在较低的lvl那个图像上。

然后可以调整内部图像的大小。

现在我有这个代码:

function ResizeImages()
{
    jQuery(document).ready(function () 
    {
        var table = $("").find()
        table.each("td")(function()
        {
            if(hassummary & summary.equals("forum"))
            {
                var img=table.find("image")
                img.height="";
                img.weight="";
            }
        }
    }
}

更新:所以这是层次结构:

<table summary="forum">
 .....
    <table>

        <table>
            <tr>
                <td class="particular class">
                    <a link>
                        <image> the one i need to get</image>
                    </a>
                </td>
            </tr>
        </table> </table></table> ....
jquery image html-table
2个回答
1
投票

用于搜索图像的jQuery代码是:

$('table[summary="forum"] td.particular.class img').attr({
    height: '',
    width: ''
});

尽管使用了这段代码,但我认为最重要的部分是理解jQuery选择器。选择器由这些子选择器组成:

  • table [summary =“forum”]使用名为<table>的属性等于summary搜索所有"forum"元素。
  • td.particular.class搜索所有<td>元素同时具有类particularclass
  • img搜索所有<img>元素。

子选择器由空格分隔。这意味着<img>元素必须放在<td>元素中,并且这些元素必须放在<table>元素中。其他详细信息在jQuery selectors的完整文档中。

找到正确的<img>元素后,可以使用attr()函数设置其属性。


4
投票

这应该这样做:

$('table[summary=forum] td.particular.class img').each(function(){
   $(this).width(w).height(h);
});
© www.soinside.com 2019 - 2024. All rights reserved.