jquery有时候工作有时不会

时间:2013-05-24 09:52:04

标签: jquery sharepoint

我尝试在sharepoint中修改外部列表的列。

alert(1) alert(2)有效。但alert(3)并不总是有效。问题是什么?这是我的代码:

 <script type="text/javascript">

    alert(1); 
    $().ready(function() { 
          alert(2);     
          $('table[summary="CalisanBilgi"] ').each(function() {
              alert(3);         
              $('td:last-child', $(this)).html('<img src="http://ahapp/img/image.aspx?s="'+$('td:last-child', $(this)).html() +'" />')  
          });
    }); 

    </script>

编辑:

这是列表的代码,我可以从浏览器中看到带有Web Dev Tools的html。 我尝试修改名为ID的最后一列,以显示该人的图像。

<table  summary="CalisanBilgi" ....>
<tbody>
<tr class=" ms-itmHoverEnabled ms-itmhover">
<td class="ms-cellStyleNonEditable ms-vb-itmcbx ms-vb-imgFirstCell">
ID
</td>
<td class="ms-cellstyle ms-vb2">
NAME
</td>
<td class="ms-vb-lastCell ms-cellstyle ms-vb2 ms-vb-lastCell">
ID
</td>
</tr>
</tbody>
</table>

解答:

$(document).ready(function () {
        $('table[summary="CalisanBilgi"] tr').find('.ms-vb-lastCell').each(function(){

           $(this).html('<img src="http://ahapp/img/image.aspx?s=' + $(this).html() + '"/>') ;       
       });  

}); 

2 个答案:

答案 0 :(得分:3)

我认为这应该做你想要的:

   $(document).ready(function() 
   { 
       $('table[summary="CalisanBilgi"] tr td:last-child').each(function()
       {
           $(this).html('<img src="http://ahapp/img/image.aspx?s=' + $(this).html() + '"/>') ;       
       });
   }); 

cfr this fiddle

有些解释词:

1)选择器table[summary="CalisanBilgi"] tr td:last-child在摘要td

的表格中选择每个tr的持续'table[summary="CalisanBilgi"] tr td:last-child'数组

2)在each()函数中,这些td中的每一个都由$(this)

标识

3)我只是获取此td的html内容,其中包含用户的ID,并使用它来生成最终替换原始内容的img标记。

虽然你会通过sharepoint(服务器)代码生成这些内容,但我会更加清楚...这会更加清晰......

答案 1 :(得分:0)

我想你忘记了分号

这里

$('td:last-child', $(this)).html('<img src="http://ahapp/img/image.aspx?s="'+$('td:last-child', $(this)).html() +'" />') ;