當我取消移動時,我想獲得我的按鈕的默認文本,但如果我定義我的默認文本按鈕上取消它給我一個操縱的文本,但我想獲得默認文本我該怎麼做?如何在懸停功能中設置默認變量?
$('button').each(function(){
var dataColor = $('this').data('color');
$(this).css({
color:dataColor,
});
$(this).hover(function(){
var defaultButtonText = $(this).text();
$(this).text('SALE');
},function(){
$(this).text(defaultButtonText);
});
})
button{
width:10%;
border:1px solid #ccc;
background:#fff;
padding:12px;
margin:5%;
cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-color="#cc0033">A button</button>
<button data-color="#396786">B button</button>
<button data-color="#CC99FF">C button</button>
申報'defaultButtonText'外'.hover()在第二個回調' –
, defaultButtonText超出了範圍,因爲您只在第一個回調中聲明瞭它。您應該閱讀JavaScript中的變量範圍。 – ADyson