ie removeAttr title problem
When i try to remove the attr title from an element it deletes it the second time i hover the element.
This problem only occurs in internet explorer.
here's the code:
$('span').live({"mouseover": function(){
clearTimeout(开发者_开发百科$(this).data('timeoutId'));
var title = $(this).attr('title');
$(this).removeAttr("title");
if($(this).children('.tip-hover').length > 0){
if($(this).children('.tip-hover').is(':visible') == false){
$(document).find('.tip-hover').hide();
$(this).children('.tip-hover').fadeIn(100);
}else{
$(document).find('.tip-hover').hide();
$(this).children('.tip-hover').show();
}
}else{
if(title != false){
var height = $(this).css('line-height') + 2;
$(this).prepend('<div class="tip-hover"><div class="tip-top"></div><div class="tip-hover-wrap"><div class="tip-hover-inner">' + title + '</div></div></div>');
$(this).children('.tip-hover').css("margin-top", height + "px");
var width = $(this).width() / 2;
$(this).children('.tip-hover').css("padding-left",width+"px");
}
}
},
"mouseout": function() {
var someelement = this;
var timeoutId = setTimeout(function(){ $(someelement).find(".tip-hover").fadeOut("slow");}, 350);
$(someelement).data('timeoutId', timeoutId);
}
});
The reason it displays the title
the first time is because you only remove the title
attribute on mouseover
. By then, it’s already too late!
I would suggest removing the title attributes outside of your event handler, as soon as the document is loaded, saving a backup in a custom data-*
attribute.
$(function() {
$('span').each(function() {
var $this = $(this);
$this
.data('title', $this.attr('title'))
.removeAttr('title');
});
});
Then, in your code, instead of…
var title = $(this).attr('title');
Use:
var title = $(this).data('title');
精彩评论