2012-05-17 232 views
0
更换一些值

我有以下的html代码:与超链接使用jQuery

<table id="MatrixTable"> 
    <tr> 
     <td id="321"> 0 </td> 
    </tr> 
</table 

答:我如何可以替换的超链接的“0”文本时,鼠标悬停使用jQuery像下面这样:

<table id="MatrixTable"> 
    <tr> 
     <td id="321"> 
      <a class="modal-dialog-link" href="Edit?matrixID=321" updatefunction="UpdateMatrix"> 
       0 
      </a> 
     </td> 
    </tr> 
</table> 

$("table#MatrixTable td").mouseover(function() { 
    // doing something here... 
}); 

B.我怎么能回到原来的“0”时,鼠标离开与jQuery像下面这样:

$("table#MatrixTable td").mouseleave(function() { 
    // doing something here... 
}); 

钍anks。

+0

为什么你希望他们只能与鼠标悬停链接。这在我眼中是没有意义的。 – HerrSerker

回答

0

您可以使用hover绑定的事件处理程序mouseentermouseleave事件,您可以使用wrapunwrap包裹在a元素的内容:

​$("#321").hover(function() { 
    $(this).contents().wrap("<a>"); 
}, function() { 
    $(this).find("a").contents().unwrap(); 
});​​​​​ 

这是working example。检查DOM,将鼠标悬停在元素上以查看更改。

这似乎是一个非常奇怪的方式来使用链接虽然......为什么不能链接总是在DOM中?

+0

我需要这个,因为我有一个大约有2500个元素的表格。如果我定义每个链接我有一个360Kb的HTML。如果我只设置一个值并用jQuery定义链接,我有一个130Kb的html。它会让人感觉到吗? – Bronzato

+0

谢谢你们。 – Bronzato

1

使用jQuery.hover

$("table#MatrixTable #321").hover(function() { 
    $(this).html('<a class="modal-dialog-link" href="Edit?matrixID=321"'+ 
      'updatefunction="UpdateMatrix">0</a>'); 
},function(){ 
    $(this).text('0'); 
});