2014-08-29 46 views
-2

用下面的鏈接問題用CSS顯示隱藏的動態ID爲」

的事情正常工作

http://jsfiddle.net/RBJ9R/

代碼上面是:

<div class="purchasing-tips"> 
<div id="slender-trigger">nnnn</div> 
        <div id="slender"></div> 
</div> 

$("#slender-trigger").on("mouseenter", function() { 
     $("#slender").show(); 
}).on("mouseleave", function() { 
     $("#slender").hide(); 
}); 

#slender-trigger { 
background-color: #CCC; 
height: 80px; 
width: 40px; 
margin-left: 70px; 
margin-top: 50px; 
position: absolute; 
} 
#slender { 
font-family: 'Strait', sans-serif; 
height: 400px; 
width: 400px; 
border: thin dotted #F00; 
position: absolute; 
margin-top: 100px; 
display: none; 
} 

現在,如果我啓用動態ID與此,它只是停止顯示懸停的div框,是否有一些問題

請指導,發送更新是在這裏

http://jsfiddle.net/RBJ9R/1292/

不起作用

+1

爲什麼ARNT你只是用CSS這一點。 .image:hover .slender {display:block} – GifCo 2014-08-29 14:33:50

+0

jQuery在你不工作的例子中沒什麼意義。當沒有一個類有類時,你可以用類似的方法來分割類。然後嘗試選擇其他不存在的元素。 – j08691 2014-08-29 14:34:33

+0

你的問題似乎是關於動態分配ID時不觸發的事件。如果在加載頁面後設置了id屬性,則事件處理程序(.on)不會綁定到該元素。你可能會發現這個鏈接對事件委託有幫助:http://learn.jquery.com/events/event-delegation/ – headlikearock 2014-08-29 14:41:07

回答

2

你的代碼沒有被正確寫入。

$(".image").on("mouseenter", function() { 
      var pop = $(this).next().show(); 

    }).on("mouseleave", function() { 
      var pop = $(this).next().hide(); 
    }); 

http://jsfiddle.net/RBJ9R/1293/

0

我會建議使用而不是你的代碼的這個

$(".image").on("mouseenter", function() { 
     $(this).find('.slender').show(); 

    }).on("mouseleave", function() { 
     $(this).find('.slender').hide(); 
    }); 
0

你的代碼不工作(除了明顯的錯誤,如選擇類,而不是原因id屬性,在重建的id中包含一個隨機連字符,並且在重建的id中不包括〜)是CSS id不能包含代字號。如果使用連字符( - )替換〜(並修復其他錯誤),那麼代碼將起作用,不過其他解決方案之一可能會更好地爲您提供服務。

$("#popup-"+pID).show(); 

http://jsfiddle.net/RBJ9R/1294/