javascript - IE 10, 11. 如何防止使用占位符的文本输入触发焦点输入事件?

标签 javascript html focus internet-explorer-11 placeholder

在 IE 11 中,如果我有一个空电子邮件 inputplaceholder ,然后单击(聚焦)它,input事件被触发。
有谁知道为什么,有解决办法吗,因为 input值(value)并没有真正改变?

var el = document.getElementById('myEmail');
el.addEventListener("input", myFunction, true);
function myFunction()
{
    alert("changed");
}
<input id="myEmail" type="email" placeholder="Email">
    

最佳答案

我参加聚会很晚,但我遇到了同样的问题,我想出了一个解决方法来解决 IE 上的这种行为。
事实上,有两个不同的错误(或者更确切地说,只有一个错误,但有两种行为,具体取决于目标是输入还是文本区域)。

  • 对于 input : 每次字段的视觉内容发生变化时都会触发该事件,包括键盘输入(自然),以及占位符出现/消失(无内容时模糊),或以编程方式更改可见占位符时。
  • 对于 textarea :基本上是一样的,只是占位符消失时事件不会触发。


  • function onInputWraper(cb) {
        if (!window.navigator.userAgent.match(/MSIE|Trident/)) return cb;
    
    
        return function (e) {
            var t = e.target,
                active = (t == document.activeElement);
            if (!active || (t.placeholder && t.composition_started !== true)) {
                t.composition_started = active;
                if ((!active && t.tagName == 'TEXTAREA') || t.tagName == 'INPUT') {
                    e.stopPropagation();
                    e.preventDefault();
                    return false;
                }
            }
            cb(e);
        };
    }
    
    var el = document.getElementById('myEmail');
    el.addEventListener("input", onInputWraper(myFunction), true);
    function myFunction() {
        alert("changed");
    }
    <input id="myEmail" type="email" placeholder="Email">


    还有一个完整的示例,您还可以在其中更改占位符值

    function onInputWraper(cb) {
      if (!window.navigator.userAgent.match(/MSIE|Trident/)) return cb;
    
    
      return function (e) {
        var t = e.target,
            active = (t == document.activeElement);
        if (!active || (t.placeholder && t.composition_started !== true)) {
          t.composition_started = active;
          if ((!active && t.tagName == 'TEXTAREA') || t.tagName == 'INPUT') {
            e.stopPropagation();
            e.preventDefault();
            return false;
          }
        }
        cb(e);
      };
    }
    
    function handle(event) {
      console.log('EVENT', event);
      document.getElementById('output')
        .insertAdjacentHTML('afterbegin', "<p>" + event.type + " triggered on " + event.target.tagName +
                            '</p>');
    }
    
    var input = document.getElementById('input'),
        textarea = document.getElementById('textarea');
    
    input.addEventListener('input', onInputWraper(handle));
    textarea.addEventListener('input', onInputWraper(handle));
    // input.addEventListener('input', handle);
    // textarea.addEventListener('input', handle);
    
    // Example's settings
    
    function removeListeners(elem) {
      var value = elem.value,
          clone = elem.cloneNode(true);
      elem.parentNode.replaceChild(clone, elem);
      clone.value = value;
      return clone;
    }
    
    document.querySelector('#settings input[type="checkbox"]').addEventListener('change', function (event) {
      if (event.target.checked) {
        document.getElementById('output').insertAdjacentHTML('afterbegin', '<p>Filter enabled !</p>');
    
        //input = removeListeners(input);
    
        console.log(input.value.length, (input == document.activeElement));
    
        input = removeListeners(input);
        input.addEventListener('input', onInputWraper(handle));
        input.composing = input.value.length > 0 || (input == document.activeElement);
    
        textarea = removeListeners(textarea);
        textarea.addEventListener('input', onInputWraper(handle));
        textarea.composing = textarea.value.length > 0 || (textarea == document.activeElement);
    
      } else {
        document.getElementById('output').insertAdjacentHTML('afterbegin', '<p>Filter disabled !</p>');
    
        input = removeListeners(input);
        input.addEventListener('input', handle);
        input.composing = void 0;
    
        textarea = removeListeners(textarea);
        textarea.addEventListener('input', handle);
        textarea.composing = void 0;
    
      }
    });
    
    document.getElementById('input_cfg').addEventListener('click', function () {
      document.getElementById('input').setAttribute('placeholder', document.getElementById(
        'input_placeholder').value);
    });
    document.getElementById('textarea_cfg').addEventListener('click', function () {
      document.getElementById('textarea').setAttribute('placeholder', document.getElementById(
        'textarea_placeholder').value);
    });
    * {
      font: 15px arial, sans-serif;
    }
    
    dd {
      background: FloralWhite;
      margin: 0;
    }
    
    dt {
      padding: 15px;
      font-size: 1.2em;
      background: steelblue;
      color: AntiqueWhite;
    }
    
    p {
      margin: 0;
    }
    
    button,
    label {
      width: 300px;
      margin: 5px;
      padding: 5px;
      float: left;
      color: DarkSlateGray;
    }
    
    #settings label {
      width: 100%;
      margin: 15px;
    }
    
    #forms input,
    #forms textarea,
    #settings input:not([type]) {
      display: block;
      width: calc(100% - 340px);
      padding: 7px;
      margin: 0;
      margin-left: 320px;
      min-height: 25px;
      border: 1px solid gray;
      background: white;
      cursor: text;
    }
    
    ::placeholder {
      /* Chrome, Firefox, Opera, Safari 10.1+ */
      color: LightBlue;
      opacity: 1;
      /* Firefox */
    }
    
    ::-ms-input-placeholder {
      /* Microsoft Edge */
      color: LightBlue;
    }
    
    :-ms-input-placeholder {
      /* Internet Explorer 10-11 */
      color: LightBlue;
    }
    <dl>
      <dt>Forms</dt>
      <dd id="forms">
        <label for="input">Input: </label>
        <input id="input" name="input" class="testing" placeholder="Type some text" />
        <label for="texarea">Textarea: </label>
        <textarea id="textarea" name="textarea" placeholder="Type some text"></textarea>
      </dd>
      <dt>Settings</dt>
      <dd id="settings">
        <p>
          <label><input type="checkbox" checked>Enable filtering script</label>
          <button id="input_cfg">Change input's placeholder to</button><input id="input_placeholder" />
        </p>
        <p>
          <button id="textarea_cfg">Change textarea's placeholder to</button>
          <input id="textarea_placeholder" />
        </p>
      </dd>
      <dt>Output</dt>
      <dd id="output"></dd>
    </dl>


    or on jsfiddle

    关于javascript - IE 10, 11. 如何防止使用占位符的文本输入触发焦点输入事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48138865/

    相关文章:

    javascript - 使用 node-debug 调试 Node js

    html - HTML 和 CSS 的空格和新行

    java - 更改 Java Swing 组件焦点指示器的颜色

    javascript - .focus() 不适用于悬停事件中的文本区域

    javascript - 错误 : [$injector:unpr] Unknown provider: ngTableParamsProvider <- ngTableParams

    javascript检测输入的字符是繁体中文还是简体中文

    javascript - Jquery UI 无法与 1.12 版本一起使用

    JQuery 问题 - 直到滚动时 Logo 才会显示

    javascript - main.css 未加载,因为它的 MIME 类型 “text/html” 不是 “text/css” ?

    wpf - 托管在 ElementHost 中的 UserControl 出现奇怪的焦点问题