我正在开发一个使用html和jquery的应用程序。它有4个按钮,当我点击它们时,它们用来执行各种任务。我现在想要的是当鼠标悬停在按钮上时,向用户显示按钮的状态。例如,对于开/关按钮,如果鼠标悬停在该按钮上,则会向用户显示该按钮当前处于关闭或打开状态。
一种方法是使用alert(),但每次我转到按钮时,它都会显示一个我不想要的警报,当鼠标悬停在按钮上时,我只想在按钮上显示一个简单的文本等。
任何人都可以帮助我,我该怎么做?
发布于 2014-04-08 02:02:37
使用jQuery .hover()
更改按钮悬停时按钮的文本
$('button').hover(function(){
$(this).text('ON');
},function(){
$(this).text('OFF');
});
Demo Fiddle
发布于 2014-04-08 02:02:52
假设您已经知道如何获取按钮的状态,让我们假设您将该状态赋给了一个名为...的变量。好吧,state
。:)
然后就很简单了:
$("button").hover(function() { // You may choose a different selector for your buttons
var curButton = this,
$curButton = $(this);
curButton.oldHTML = $curButton.html(); // Cache old button label
$curButton.html(state); // Change button label to reflect state
}, function() {
var curButton = this,
$curButton = $(this);
$curButton.html(curButton.oldHTML); // Restore old HTML
});
https://stackoverflow.com/questions/22919702
复制相似问题