【问题标题】:HTML how to keep focuing state of input after clicking another element(eg button)HTML如何在单击另一个元素(例如按钮)后保持输入的焦点状态
【发布时间】:2019-11-26 03:28:58
【问题描述】:

我想在单击按钮后保持对焦状态。保持状态意味着:

  • 如果<input/>有焦点,点击按钮后保持焦点;
  • 如果<input/>之前没有聚焦,点击按钮后不要聚焦

所以,点击后,由于<input/> 的当前对焦状态始终为“不对焦”,我想获取<input/> 之前的对焦状态。

问题是blur 总是在click 事件之前被调用。

我尝试过的:

// onclick event
this.$iptPassword.on('click', this.toggleShowPassword);
// callback
toggleShowPassword() {
    console.log(this.$iptPassword.is(':focus')); // always return false
    this.$iptPassword.attr('type', this.$iptPassword.attr('type') === 'password' ? 'text' : 'password');
}

【问题讨论】:

  • Element.focus()
  • 1.没有 HTML 元素,也许您想改用.click .img-eye? 2. 请在工作的 sn-p 上提供完整的 HTML 3. 添加 jquery 标签 4. console.log($iptPassword); 有想要的结果吗?

标签: javascript jquery html


【解决方案1】:

创建了一个有效的 Plnkr - Toggle input type + Focus state

引入了 2 个变量 - “inputWasFocused”和“clickWasOutsideOfScope” - 使用这些变量,我可以保存最后一个输入状态(聚焦/未聚焦) - 在实际的“模糊”事件之前

【讨论】:

    【解决方案2】:

    这里有类似的问题。但触摸屏的情况要复杂得多。

    jQuery: fire click() before blur() event

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-09
      • 2017-03-16
      • 1970-01-01
      • 2016-10-19
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多