【问题标题】:How to trigger two effects on a single event in javascript如何在javascript中对单个事件触发两种效果
【发布时间】:2013-09-16 03:28:32
【问题描述】:

我希望这两个效果在不使用函数的情况下发生,有什么语法吗?

onkeypress="this.value=(this.value=='Password')?'' : this.value"

onkeypress="this.type='password'"

【问题讨论】:

  • a) 将它们放在同一个处理程序中 b) 使用两个处理程序 attaching them properly
  • 告诉我@Bergi 的路
  • 你应该简单地使用<input type="password" placeholder="Password">
  • 哈哈,它解决了我的问题,但我仍然想知道有没有办法以这种方式做?
  • 为什么每次按键都需要将type 更改为password?一旦你设置它就不会变回来。

标签: javascript html dom


【解决方案1】:

<input type="password" placeholder="Password"> 将更适合您正在尝试做的事情。但是,要回答您的问题:只需将两个语句放在同一个处理程序中:

onkeypress="if (this.value=='Password') this.value = ''; this.type = 'password';"

请注意,您将获得problems with Internet Explorer,它不允许更改输入元素的type。另见this answer。

【讨论】:

  • 完美 :) 非常感谢
【解决方案2】:

嘿,好问题。

onclick="checkPassword();"

然后有一个 JavaScript(Bergi 提到的):

var pw = document.getElementById("password");

if (pw.value=='Password') pw.value = ''; pw.type = 'password';

HTML:

<input type="password" id="password"/>

【讨论】:

  • thanx 但我没有提到任何功能,而 bergi 以其他方式回答的几乎相同。
  • 因为我知道如何使用函数,所以占位符更好@Bergi
猜你喜欢
  • 1970-01-01
  • 2019-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
  • 2011-01-30
相关资源
最近更新 更多