【问题标题】:How can I add focus() and set placeholder text on click event如何在点击事件上添加焦点()并设置占位符文本
【发布时间】:2017-09-29 05:58:48
【问题描述】:

我正在向我的搜索图标添加一个事件侦听器以侦听“单击”事件,然后在触发时调用一个函数,该函数将焦点添加到元素并将占位符属性更改为“输入您的搜索词...”。当前,当单击图标时,输入显示为焦点,但没有占位符文本。我错过了什么?

 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 );
        };

【问题讨论】:

标签: javascript


【解决方案1】:

你用的是什么浏览器? IIRC,IE(至少 11 个,不知道“Edge”)将在字段获得焦点后立即隐藏占位符。 (不过,Chrome 做到了这一点。)

【讨论】:

  • 我正在使用 Chrome。
猜你喜欢
  • 2015-08-15
  • 2012-09-14
  • 2014-03-31
  • 2018-08-08
  • 2011-02-12
  • 2012-02-23
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多