[英]How can I add focus() and set placeholder text on click event
我在搜索圖標中添加了一個事件偵聽器,以偵聽“單擊”事件,然后在觸發時調用將焦點添加到元素並將占位符屬性更改為“輸入搜索詞...”的函數。 當前,單擊圖標時,輸入顯示為具有焦點,但沒有占位符文本。 我想念什么?
window.onload = function() {
var el = document.getElementById('gsc-i-id1');
el.setAttribute('placeholder', 'Enter your search term...');
el.style.background = '';
el.style.textIndent = '0';
el.addEventListener('blur', function(e) {
e.target.style.backgroundImage = 'none';
e.target.style.textIndent = '0';
}, false );
var searchIcon = document.getElementById('search-icon');
searchIcon.addEventListener('click', function(e) {
e.preventDefault();
el.focus( function() {
el.setAttribute('placeholder', 'Enter your search term...');
});
}, false );
};
你使用的是什么瀏覽器? 字段獲得焦點后,IIRC,IE(至少11個,不知道“ Edge”)將隱藏占位符。 (不過Chrome正確無誤。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.