我正在尝试在悬停时输出链接的“alt”。 (文本显示在#container
div)。
这是我到目前为止所尝试的(它不起作用):
如果您有任何更好的想法,请提出建议。
HTML
<div><a href="#" class="heroes" alt="necromancer">Icon</a></div>
<div><a href="#" class="heroes" alt="witch">Icon</a></div>
<div><a href="#" class="heroes" alt="barbarian">Icon</a></div>
<div><a href="#" class="heroes" alt="troll">Icon</a></div>
<div id="container"></div>
JS:
$('.container').hover(function() {
var hero = $(this).attr('alt');
$('#container').text(hero);
}, function() {
$('#container').text("");
});
JSFIDDLE:http://jsfiddle.net/XDky6/
答案 0 :(得分:1)
$('.heroes').hover(function() {
var hero = $(this).attr('alt');
$('#container').text(hero);
}, function() {
$('#container').text("");
});
应在hover
上调用 $('.heroes')
,而不是$('.container')
。
答案 1 :(得分:1)
您错误输入了代码。 你指的是错误的班级。
它应该是.heroes
,但它是.container
。
固定代码:
$('.heroes').hover(function() {
var hero = $(this).attr('alt');
$('#container').text(hero);
}, function() {
$('#container').text("");
});
答案 2 :(得分:1)
$('.heroes').hover(
function() {
var hero = $(this).attr('alt');
$('#container').text(hero);
},
function() {
$('#container').text("");}
});
答案 3 :(得分:0)
工作演示 http://jsfiddle.net/RdSCV/1/
传递了错误的类,即正确的类是.heroes
<强>码强>
$('.heroes').hover(function() {
var hero = $(this).attr('alt');
$('#container').text(hero);
}, function() {
$('#container').text("");
});
答案 4 :(得分:0)
初始选择器不正确。将.container
更改为.heroes
答案 5 :(得分:0)
如果您可以接受链接旁边显示的alt
文字,那么:
<style>
a:hover:after {
content: attr(alt);
position: absolute;
background: #fff;
padding: 2px;
border: 1px solid #999;
box-shadow: 1px 1px 5px #bbb;
}
</style>