我會使用jQuery .hover()和jQuery的組合.data():
http://jsfiddle.net/5W4Bd/
HTML:
<div id="myDiv">default text</div>
的javascript:
$('#myDiv')
.data('textToggle', 5)
.hover(function (e) {
var that = $(this);
// get the text from data attribute
var textToSet = that.data('textToggle');
// save the current text so it can be reverted
that.data('textToggle', that.text());
// set the new text
that.text(textToSet);
}, function (e) {
var that = $(this);
// get the text from data attribute
var textToSet = that.data('textToggle');
// save the current text so it can be reverted
that.data('textToggle', that.text());
// set the new text
that.text(textToSet);
});
編輯:隨意重構匿名functi因爲它們完全一樣:)
這聽起來像一個糟糕的設計。用戶如何猜測他們應該懸停文本,告訴他們有多少回覆才能發起自己的回覆?不要依賴文本本身,而是將其包裹在一個範圍內並定位該元素。 – MetalFrog 2012-03-28 18:12:55
@MetalFrog文字有一個按鈕的外觀,我不認爲它會花費太多時間來理解。 – Sophivorus 2012-03-28 18:16:32