2013-04-05 141 views
-1

先生, 我需要使用jQuery的标签tdtrdiv包含onmouseoutonmouseover添加元素添加属性。的jQuery中的元素TR,TD和DIV

如果发现onmouseout="ANY FUNCTION"然后我需要添加属性onblur="SAME FUNCTION AS onmouseout"

那么,如果发现onmouseover="Any Function"然后我需要添加属性onfocus="sane functions as onmouseover"

此功能包含在标签td, tr or div

如TD:

<td onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="zz1_GS"> 

然后我需要:

<td onmouseover="Menu_A(this)" onfocus="Menu_A(this)" onmouseout="Menu_Unhvr(this)" onblur="Menu_Unhvr(this)" onkeyup="Menu_Key(this)" id="zz1_GS"> 

例TR:

<tr onmouseover="Menu_HoverDynamic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="zz86756f"> 

那么我需要像这样:

<tr onmouseover="MDynamic(this)" onfocus="MDynamic(this)" onmouseout="Menu_r(this)" onblur="Menu_r(this)" onkeyup="Menu_Ky(this)" id="zz86756f"> 

例格:

<div class="1_5656" id="d867444f" onmouseover="PopOUp(this)" onmouseout="PopOStop(this)" style="text-align:center;"> 

然后我需要的东西是这样的:

<div class="1_5656" id="d867444f" onmouseover="PopOUp(this)" onfocus="PopOUp(this)" onmouseout="PopOStop(this)" onblur="PopOStop(this)" style="text-align:center;"> 

ID名称不一样而td和tr的水平并不一致。

谁能帮我实现这一目标?

我做这个代码,但它不工作:

  $('div[onmouseout]').each(function() { 
     $(this).attr('onblur', $(this).attr('onmouseout')); 
     } 

     $('div[onmouseover]').each(function() { 
     $(this).attr('onfocus', $(this).attr('onmouseover')); 
     } 

     $('tr[onmouseout]').each(function() { 
     $(this).attr('onblur', $(this).attr('onmouseout')); 
     } 

     $('tr[onmouseover]').each(function() { 
     $(this).attr('onfocus', $(this).attr('onmouseover')); 
     } 

     $('td[onmouseout]').each(function() { 
     $(this).attr('onblur', $(this).attr('onmouseout')); 
     } 

     $('td[onmouseover]').each(function() { 
     $(this).attr('onfocus', $(this).attr('onmouseover')); 
     } 
+0

你试过了什么? 。 – Vladimirs 2013-04-05 14:34:06

+0

令人困惑的问题,不能ü阐述:( – dreamweiver 2013-04-05 14:34:26

+1

请不要使用'onwhatever'属性触发JavaScript的它更容易,从长远来看,要添加使用['。对()']的处理程序(HTTP:// API .jquery.com/on) – Blazemonger 2013-04-05 14:35:59

回答

0

它实际上可以这样做只是通过复制的属性,并使用空属性选择抓取与特定属性的所有元素:

var $mo = $('[onmouseover]'), 
    fstr = $mo.attr('onmouseover'), 
    bstr = $mo.attr('onmouseout'); 
$mo.attr('onfocus',fstr).attr('onblur',bstr); 
+0

我试过它没有为我工作 – MANI 2013-04-05 15:19:30

0

试试这个

$("div, tr, td").not("[onmouseout='']").each(function() { 
    $(this).attr('onblur', $(this).attr('onmouseout')); 
}); 

$("div, tr, td").not("[onmouseover='']").each(function() { 
    $(this).attr('onfocus', $(this).attr('onmouseover')); 
}); 

更新。 工作正常,只是检查。重新检查我的代码,找出你失败的地方:

<html> 
<head> 
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
<script> 
function PopOUp() {} 
function PopOStop() {} 

$(function() { 
    $("div, tr, td").not("[onmouseout='']").each(function() { 
     $(this).attr('onblur', $(this).attr('onmouseout')); 
    }); 

    $("div, tr, td").not("[onmouseover='']").each(function() { 
     $(this).attr('onfocus', $(this).attr('onmouseover')); 
    }); 
}); 
</script> 
</head> 
<body> 
<div class="1_5656" id="d867444f" onmouseover="PopOUp(this)" onmouseout="PopOStop(this)" style="text-align:center;"></div> 

<table onmouseover="PopOUp(this)" onmouseout="PopOStop(this)"> 
    <tr onmouseover="PopOUp(this)" onmouseout="PopOStop(this)"> 
     <td onmouseover="PopOUp(this)" onmouseout="PopOStop(this)"></td> 
    </tr> 
</table> 
</body> 
</html> 
+0

我试过它没有工作 – MANI 2013-04-05 15:20:17

+0

我已经更新了答案。 – Vladimirs 2013-04-05 15:33:31

+0

我试图复制粘贴你的代码在html文件中并运行它。在我查看源代码后,它没有在div,table,tr和td中添加onblur或onfocus元素。 – MANI 2013-04-07 03:06:58