【发布时间】:2009-12-12 17:34:06
【问题描述】:
我有很多 div,我想淡化这个悬停的。
我如何获得悬停的 div 的 id?
除了使用“onmouseover”调用函数(并发送id)之外,还有其他方法吗?
谢谢!
【问题讨论】:
我有很多 div,我想淡化这个悬停的。
我如何获得悬停的 div 的 id?
除了使用“onmouseover”调用函数(并发送id)之外,还有其他方法吗?
谢谢!
【问题讨论】:
试试类似的东西
$(".classes").mouseover(function() {
$(this).function();
};
使用 attr 函数获取元素的 ID
$('.name').attr('id');
【讨论】:
悬停在这些 div 上...
HTML:
<div class="add_post_cmmnt" id="box-100" >Box1</div>
<div class="add_post_cmmnt" id="box-200" >Box2</div>
<div class="add_post_cmmnt" id="box-400" >Box3</div>
JavaScript:
$(".add_post_cmmnt").hover(function(e) {
var id = this.id; // Get the id of this
console.log("id is " + id); //Test output on console
$('#pid').val(id); // Set this value to any of input text box
$(this).fadeOut(400); // Finally Fadeout this div
});
【讨论】:
您可以在应用动画后设置一个类来标记 div,以便您可以轻松识别悬停的 div。
(function($){
$.fn.extend({
myDivHover: function(){
var $set = $(this);
return $set.each(function(){
var $el = $(this);
$el.hover(function(){
fadeOutAnimation( $el, $set );
}, function(){
fadeInAnimation( $el );
});
});
}
});
function fadeOutAnimation( $target, $set ){
// Revert any other faded elements
fadeInAnimation( $set.filter('.hovered') );
// Your fade code here
...
...
// Flag
$target.addClass('hovered');
}
function fadeInAnimation( $target ){
// You revert fade code here
...
...
// Unflag
$target.removeClass('hovered');
}
})(jQuery);
// Apply it to the divs with XXX class
$('div.XXX').myDivHover();
// Select hovered item
var theID = $('div.XXX').filter('.hovered').attr('id');
希望对你有帮助:)
【讨论】: