Javascript ie removeAttr标题问题
当我试图从元素中删除attr title时,它会在我第二次悬停该元素时将其删除 此问题仅在internet explorer中发生 代码如下:Javascript ie removeAttr标题问题,javascript,jquery,internet-explorer,Javascript,Jquery,Internet Explorer,当我试图从元素中删除attr title时,它会在我第二次悬停该元素时将其删除 此问题仅在internet explorer中发生 代码如下: $('span').live({"mouseover": function(){ clearTimeout($(this).data('timeoutId')); var title = $(this).attr('title'); $(this).removeAttr("title"); if($(this).child
$('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);
}
});
$('span').live({“mouseover”:function(){
clearTimeout($(this.data('timeoutId'));
var title=$(this.attr('title');
$(此).removeAttr(“标题”);
if($(this).children('.tip hover')。长度>0){
if($(this).children('.tip hover').is(':visible')==false){
$(文档).find('.tip hover').hide();
$(this).children('.tip hover').fadeIn(100);
}否则{
$(文档).find('.tip hover').hide();
$(this.children('.tip hover').show();
}
}否则{
如果(标题!=错误){
var height=$(this.css('line-height')+2;
$(this).prepend(“”+title+“”);
$(this).children('.tip hover').css(“页边距顶部”,高度+“px”);
var width=$(this).width()/2;
$(this).children('.tip hover').css(“左填充”,宽度+“px”);
}
}
},
“mouseout”:函数(){
var someelement=这个;
var timeoutId=setTimeout(function(){$(someelement).find(.tip hover”).fadeOut(“slow”);},350);
$(someelement).data('timeoutId',timeoutId);
}
});
它第一次显示标题的原因是因为您只删除了鼠标上方的标题
属性。到那时,已经太晚了
我建议在加载文档后立即删除事件处理程序之外的title属性,并将备份保存在自定义data-*
属性中
$(function() {
$('span').each(function() {
var $this = $(this);
$this
.data('title', $this.attr('title'))
.removeAttr('title');
});
});
然后,在代码中,而不是
var title = $(this).attr('title');
使用:
jQuery 101:将对$(this)的引用存储到变量中,这样就不必一直查找它$()的开销很大。另外,如果(title!=false)
永远不会为false,除非有什么东西将其设置为布尔false值。删除属性不会为该提示生成false.thanx!把它改对了!我该怎么做呢?检查标题是否存在!为什么要使用$(document.find('.tip hover')
?$('.tip hover')
怎么了?我想这是个坏习惯!虽然我已经使用jquery很长时间了,但每次遇到问题时我都会学到新东西!我想这是件好事。如果在存储数据时在文档加载时预先添加div,会更好吗?
var title = $(this).data('title');