1

以下に示す html テーブルとスクリプトを考えると、マウスを行の外に移動しなくても、マウスが入った直後にマウスを離すイベントが発生するように見えるという問題があります。

<script type="text/javascript" language="javascript">
    function highlightRows(iMainID) 
    {
        $('tr[mainid=' + iMainID+ ']').each(function() {
            if ($(this).attr('old') == undefined) {
                $(this).attr('old', $(this).css('backgroundColor'));
            }
            $(this).animate({ backgroundColor: "#FFFFCC" }, 500);
            $(this).mouseout(function() {
                if ($(this).attr('old') != undefined) {
                    $(this).animate({ backgroundColor: $(this).attr('old') }, 500);
                }
            });
        });        
    }
</script>
<table>
    <tr> 
      <td mainid="1" onmouseover='highlightRows(1)'><div>text</div></td>
      <td mainid="1" onmouseover='highlightRows(1)'><div>text</div></td>      
      <td mainid="2" onmouseover='highlightRows(2)'><div>text</div></td>
    </tr>
<table>
4

4 に答える 4

3

PointyとJerの両方が示しているように、一方のモデル(JQuery)またはもう一方のモデル(HTMLのWhatever)を選択する必要があり、それらを混在させないでください。

ほとんどの場合、複式簿記は、同じイベントに複数回サブスクライブしているという事実に関係しています。(マウスオーバーを2回実行すると、両方が呼び出される2つのmouseoutイベントハンドラーが取得されます。)

また、重複する「mainid」値に注意してください。それは問題のように見え、問題の原因である可能性があります。

于 2010-03-08T19:08:23.683 に答える
1

それを行うjqueryの方法は、関数に設定されたhover$(document).readyを使用するだけで、 @pointyが言ったように、onmouseoverすべてを一緒に忘れることです

$(document).ready(function(){
    $('tr').hover(function() {
       if ($(this).attr('old') == undefined) {
          (this).attr('old', $(this).css('backgroundColor'));
       }
       $(this).animate({ backgroundColor: "#FFFFCC" }, 500);
    }, function() {
       if ($(this).attr('old') != undefined) {
           $(this).animate({ backgroundColor: $(this).attr('old') }, 500);
       }
    });
});
于 2010-03-08T19:09:08.803 に答える
1

.hover を使用しないのはなぜですか?

$('tr[mainid=' + iMainID+ ']').hover(
        function()
        {
            $(this).addClass('hoverClass');
        },
        function()
        {
            $(this).removeClass('hoverClass');
        }
    );
于 2010-03-08T19:10:01.047 に答える
0

あなたはおそらく次のようなことをしたいと思うでしょう:

function yourMouseOver(){...}
function yourMouseOut(){...}

と:

<td onmouseover="yourMouseOver()" onmouseout="yourMouseOut()">html</td>

onmouseoutイベントが発生するたびにイベントを設定するのonmouseoverは冗長です。

于 2010-03-08T19:03:32.510 に答える