是,JavaScript是這裏不錯,雖然,功能保持屬於它的地方,對事件作出響應在CSS是可疑的做法。您需要按鍵事件進行輸入。單獨定義的功能使它們更易於重複使用。
var hideExplain = function() {
document.getElementById('explain').style.opacity='0';
}
document.getElementById('searchdevice').addEventListener("keypress", hideExplain);
see keypress example here
您可能會更好,雖然這樣做,因爲焦點和模糊將允許您當用戶移動上撤消效果。這裏也有一個演示功能。
var showExplain = function() {
document.getElementById('explain').style.opacity='1';
}
document.getElementById('searchdevice').addEventListener("focus", hideExplain);
document.getElementById('searchdevice').addEventListener("blur", showExplain);
see the example here
你可以使用按鍵刪除尖端和模糊,以重新顯示它,這樣尖端會流連儘可能長的用戶。 See anothe example
此外,你會發現它更好添加和刪除類 - 這是一個與JQuery的例子。現在你的風格類也可以重用。
CSS
.is-transparent {
opacity: 0;
}
.is-opaque {
opacity: 1;
}
JQuery的
$('#explain').removeClass('is-opaque').addClass('is-transparent');
你想的不透明度爲零,如果有人打字,當有人輸入或在兩種情況下? –