2013-11-26 70 views
0

我想對鼠標輸入的一個div起作用。 我的jquery & html在下面,但它總是第一個效果。將事件處理程序分配給元素集合

網站鏈接: http://mcr.tw/stu/gjun/millie/out/tsutaya/movies/test.html

我的jQuery:

$(document).ready(function(e) 
{ 
var array=["ic_evaluation_type_b26.png"] 
var imgno=0 

var array2=["ic_forbid_off.png"] 
var imgno2=0 

for (i = 0; i < 10; i++) 
{ 
    $(".itemEvaluation .a:eq(" + i + ")").bind("mouseenter", {id: i}, fn) 
    $(".a:eq(" + i + ")").bind("click", {id: i}, fnimgno) 
} 

function fn(e) 
{ 
    no = e.data.id + 1 
    $(this) 
    .closest('.itemEvaluation') 
    .find('ul li.lili img') 
    .attr("src", "images/ic_evaluation_type_a" + no + ".png") 

    $(".itemInterest img").attr("src", "images/ic_forbid_off.png") 

} 
function fnimgno(e) 
{ 
    a = e.data.id+1 
    array=["ic_evaluation_type_a"+a+".png"] 
    imgno=0 
    array2=["ic_forbid_off.png"] 
    imgno2=0 
} 

$(".a").mouseout(function() 
{ 
    $(".itemEvaluation > ul > li:eq(1) > img").attr("src", "images/"+array[imgno]) 
    $(".itemInterest img").attr("src", "images/"+array2[imgno2]) 
}) 

$(".itemInterest img").mouseenter(function() 
{ 
    $(".itemInterest img").attr("src", "images/ic_forbid_on.png") 
    $(".itemEvaluation > ul > li:eq(1) > img").attr("src", "images/ic_evaluation_none_m.png") 
}) 

$(".itemInterest img").click(function() 
{ 
    $(".itemInterest img").attr("src", "images/ic_forbid_on.png") 
    $(".itemEvaluation > ul > li:eq(1) > img").attr("src", "images/ic_evaluation_none_m.png") 
    array=["ic_evaluation_none_m.png"] 
    imgno=0 
    array2=["ic_forbid_on.png"] 
    imgno2=0 
}) 

$(".itemInterest img").mouseout(function() 
{ 
    $(".itemInterest img").attr("src", "images/"+array2[imgno2]) 
    $(".itemEvaluation > ul > li:eq(1) > img").attr("src", "images/"+array[imgno]) 
}) 

}); 

我的HTML:

<div class="itemEvaluation"> 
    <ul class="clearfix" style="margin-bottom:7px;"> 
    <li class="itemInterest" style="margin-left:50px"> <a href="javascript:;"> <img name="interest2137585211_01" src="images/ic_forbid_off.png" title="" alt=""/> </a> </li> 
    <li class="lili"> <img alt="" title="" src="images/ic_evaluation_type_b26.png" usemap="#map_img2137585211_0" name="img2137585211_0" border="0" /> 
     <map name="map_img2137585211_0" style="display:inline"> 
     <area class="a" shape="rect" coords="0, 0, 10.3, 19" /> 
     <area class="a" shape="rect" coords="10.3, 0, 20.6, 19" /> 
     <area class="a" shape="rect" coords="20.6, 0, 30.900000000000002, 19" /> 
     <area class="a" shape="rect" coords="30.900000000000002, 0, 41.2, 19" /> 
     <area class="a" shape="rect" coords="41.2, 0, 51.5, 19" /> 
     <area class="a" shape="rect" coords="51.5, 0, 61.8, 19" /> 
     <area class="a" shape="rect" coords="61.8, 0, 72.1, 19" /> 
     <area class="a" shape="rect" coords="72.1, 0, 82.39999999999999, 19" /> 
     <area class="a" shape="rect" coords="82.39999999999999, 0, 92.69999999999999, 19"/> 
     <area class="a" shape="rect" coords="92.69999999999999, 0, 102.99999999999999, 19"/> 
     </map> 
    </li> 
    </ul> 
    <div class="clearfloat"></div> 
</div> 


<div class="itemEvaluation"> 
    <ul class="clearfix" style="margin-bottom:7px;"> 
    <li class="itemInterest" style="margin-left:50px"> <a href="javascript:;"> <img name="interest2137585211_01" src="images/ic_forbid_off.png" title="" alt=""/> </a> </li> 
    <li class="lili"> <img alt="" title="" src="images/ic_evaluation_type_b26.png" usemap="#map_img2137585211_0" name="img2137585211_0" border="0" /> 
     <map name="map_img2137585211_0" style="display:inline"> 
     <area class="a" shape="rect" coords="0, 0, 10.3, 19" /> 
     <area class="a" shape="rect" coords="10.3, 0, 20.6, 19" /> 
     <area class="a" shape="rect" coords="20.6, 0, 30.900000000000002, 19" /> 
     <area class="a" shape="rect" coords="30.900000000000002, 0, 41.2, 19" /> 
     <area class="a" shape="rect" coords="41.2, 0, 51.5, 19" /> 
     <area class="a" shape="rect" coords="51.5, 0, 61.8, 19" /> 
     <area class="a" shape="rect" coords="61.8, 0, 72.1, 19" /> 
     <area class="a" shape="rect" coords="72.1, 0, 82.39999999999999, 19" /> 
     <area class="a" shape="rect" coords="82.39999999999999, 0, 92.69999999999999, 19"/> 
     <area class="a" shape="rect" coords="92.69999999999999, 0, 102.99999999999999, 19"/> 
     </map> 
    </li> 
    </ul> 
    <div class="clearfloat"></div> 
</div> 
+0

嗯,我還沒有給你答案,但我可以指出一個非常大的問題:你將未定義的函數(out,over)分配給區域元素上的事件處理程序--onmouseover,onmousemove,onmouseout。這些函數沒有定義,並且在事件觸發時引發異常。 –

+0

謝謝,Zachary Carter! – user3037271

+0

謝謝,扎卡里卡特!這些事件onmouseover,onmousemove,onmouseout是不需要的。我已經刪除它們。 – user3037271

回答

0

好吧,我想更好地瞭解你正在試圖做什麼這裏...你的事件處理ERS應該是這個樣子:

$(".itemInterest img").mouseout(function() 
    { 
     $(this).attr("src", "images/"+array2[imgno2]); 
     $(this).parent().parent().next().children('img').attr("src", "images/"+array[imgno]); 
    }) 

$(".itemInterest img").mouseenter(function() 
    { 
     $(this).attr("src", "images/ic_forbid_on.png"); 
     $(this).parent().parent().next().children('img').attr("src", "images/ic_evaluation_none_m.png"); 
    }) 

如果要應用邏輯在事件處理程序,只有某些元素共享類,你需要確保你選擇是選擇正確的元素。

現在如果你想執行對所有元素的邏輯選擇匹配,你需要確保你遍歷由您選擇返回的集合,是這樣的:

$(".itemInterest img").mouseenter(function() 
    { 
     $(".itemInterest img").each(function(index, element) { 
      $(this).attr("src", "images/ic_forbid_on.png"); 
     }); 
     $(".itemEvaluation > ul").each(function(index, element) { 
      $(this).children('.lili').each(function(index, element) { 
       $(this).children('img').each(function(index, element) { 
        $(this).attr("src", "images/ic_evaluation_none_m.png"); 
       }); 
      }); 
     }); 
    }) 

    $(".itemInterest img").mouseout(function() 
    { 
     $(".itemInterest img").each(function(index, element) { 
      $(this).attr("src", "images/"+array2[imgno2]); 
     }); 
     $(".itemEvaluation > ul").each(function(index, element) { 
      $(this).children('.lili').each(function(index, element) { 
       $(this).children('img').each(function(index, element) { 
        $(this).attr("src", "images/"+array[imgno]); 
       }); 
      }); 
     }); 
    }) 

對不起,所有的編輯,但希望這可以爲你澄清事情。

0

如果您想將jQuery事件處理程序附加到一個特定元素 - 給該元素一個ID並使用ID選擇器而不是類選擇器。

相關問題