2013-04-05 93 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