[英]Is it possible to hide title when hovering image?
这是我的代码:
<img src="image.jpg" alt="" title="some title" />
当我悬停该图像时,会出现“某个标题”。 有没有可能被隐藏?
正如一些答案指出的那样,这很容易完成,但是知道您想对标题做什么,我会使用 data-* 自定义属性或使用 .data() 附加标题
<img src="image.jpg" alt="" data-title="some title" />
要么
$('img').data('title', 'some title');
删除title
属性很简单,但是“隐藏它”是不可能的(据我所知); 为了删除标题,以下应该有效,但目前尚未测试:
$('img').hover(
function(){
$(this).data('originalTitle',$(this).title());
$(this).removeAttr('title');
},
function(){
$(this).attr('title',$(this).data('originalTitle');
});
在上面我选择移动属性,然后在鼠标移开时替换它。
要永久删除title
:
$('img').removeAttr('title');
参考:
attr()
。data()
。hover()
。removeAttr()
。您可以将其移动到图像数据...然后返回。 像那样
$('img').hover(
function () {
$(this).data('title',$(this).attr('title')).removeAttr('title');
},
function () {
$(this).attr('title',$(this).data('title'));
}
);
如果只是悬停的问题,可以制作pointer-events: none;
在图像上,它将解决问题。
是的! 使用 jQuery,您可以在鼠标悬停时将其删除并在鼠标移出时再次添加它 -
$(function(){
var ttext;
$('img').hover(function(){
ttext = $(this).attr('title');
$(this).removeAttr('title');
},
function(){
$(this).attr('title', ttext);
});
});
不,不是真的。 但是你可以用 JavaScript 替换它,或者把它留空。
jQuery 示例:
$('[title]').removeAttr('title');
有点搞笑的是,在 Stack Overflow 上,五个人可以同时给出相同的答案:P
$(function(){
$('img').hover(function(){
$(this).removeAttr('title');
}, function(){
$(this).attr('title','some title');
});
});
我至少在 Chrome 上尝试并使用 jQuery / jQuery UI 进行了一些工作:
''
。我的代码中的示例,发生在命名的 HTML 元素可用之后:
jQuery(document).tooltip();
for(var index = 0; index < sites.length; ++index) {
jQuery('#' + sites[index][0]).attr('title', '');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.