Javascript 调用不同参数的函数

Javascript 调用不同参数的函数,javascript,jquery,html,Javascript,Jquery,Html,我正在用html和javascript制作一个表单。但是当我点击输入文本时,我想改变焦点。我已经构建了一个函数,但是如何使这个函数成为全局函数,以便将其用于其他输入文本…而不是为每个输入文本构建函数 我的职能是: function changefocus() { var pass = document.getElementById('pass_id'); if (pass.value == 'Password' ){ pass.value = ''; pa

我正在用html和javascript制作一个表单。但是当我点击输入文本时,我想改变焦点。我已经构建了一个函数,但是如何使这个函数成为全局函数,以便将其用于其他输入文本…而不是为每个输入文本构建函数

我的职能是:

function changefocus() {
    var pass = document.getElementById('pass_id');
        if (pass.value == 'Password' ){
    pass.value = '';
    pass.style.color='#000'

        }}
html的一部分是:

<input type="text" id="pass_id" name="pass" value="Password" required="required"
        onfocus="changefocus()">
然后我更改html部分,如下所示:

  <input type="text" id="pass_id" name="pass" value="Password" required="required"
            onfocus="changefocus(pass_id)">

你能帮我…我怎么解决这个问题?提前谢谢

现在,我希望当我不写任何东西而离开输入文本时,输入文本不要为空……我尝试使用onchange(),如下所示,但它确实起作用:

function fillemptyform(pass2) {

        if (pass2.value.length==0 ){
    pass2.value = 'Password';
    pass2.style.color='#000';

        }}

<input type="text" id="pass_id" name="pass" value="Password" required="required"
        onfocus="changefocus(this)" onchange="fillemptyform(this)">
函数fillemptyform(pass2){
if(pass2.value.length==0){
pass2.value='Password';
pass2.style.color='#000';
}}

如何执行此操作?

在调用
changefocus
函数时需要引用ID,而在使用参数变量时不需要引用ID

function changefocus(a) {
    var pass = document.getElementById(a);
    if (pass.value == 'Password' ){
        pass.value = '';
        pass.style.color='#000'
    }
}

<input type="text" id="pass_id" name="pass" value="Password" required="required"
        onfocus="changefocus('pass_id')">

调用
changefocus
函数时需要引用ID,而使用参数变量时不需要引用ID

function changefocus(a) {
    var pass = document.getElementById(a);
    if (pass.value == 'Password' ){
        pass.value = '';
        pass.style.color='#000'
    }
}

<input type="text" id="pass_id" name="pass" value="Password" required="required"
        onfocus="changefocus('pass_id')">

调用
changefocus
函数时需要引用ID,而使用参数变量时不需要引用ID

function changefocus(a) {
    var pass = document.getElementById(a);
    if (pass.value == 'Password' ){
        pass.value = '';
        pass.style.color='#000'
    }
}

<input type="text" id="pass_id" name="pass" value="Password" required="required"
        onfocus="changefocus('pass_id')">

调用
changefocus
函数时需要引用ID,而使用参数变量时不需要引用ID

function changefocus(a) {
    var pass = document.getElementById(a);
    if (pass.value == 'Password' ){
        pass.value = '';
        pass.style.color='#000'
    }
}

<input type="text" id="pass_id" name="pass" value="Password" required="required"
        onfocus="changefocus('pass_id')">

将此作为参数传递给函数<代码>此将分配触发事件的元素

HTML

   <input type="text" id="pass_id" name="pass" value="Password" required="required"
            onfocus="changefocus(this)">

JS Fiddle:

将此作为参数传递给函数<代码>此将分配触发事件的元素

   <input type="text" id="pass_id" name="pass" value="Password" required="required"
            onfocus="changefocus(this)">
HTML

   <input type="text" id="pass_id" name="pass" value="Password" required="required"
            onfocus="changefocus(this)">

JS Fiddle:

将此作为参数传递给函数<代码>此将分配触发事件的元素

   <input type="text" id="pass_id" name="pass" value="Password" required="required"
            onfocus="changefocus(this)">
HTML

   <input type="text" id="pass_id" name="pass" value="Password" required="required"
            onfocus="changefocus(this)">

JS Fiddle:

将此作为参数传递给函数<代码>此将分配触发事件的元素

   <input type="text" id="pass_id" name="pass" value="Password" required="required"
            onfocus="changefocus(this)">
HTML

   <input type="text" id="pass_id" name="pass" value="Password" required="required"
            onfocus="changefocus(this)">

JS Fiddle:

看起来您正在实现一个占位符。为什么不直接使用HTML5的占位符属性呢?它看起来像是在实现占位符。为什么不直接使用HTML5的占位符属性呢?它看起来像是在实现占位符。为什么不直接使用HTML5的占位符属性呢?它看起来像是在实现占位符。你为什么不直接使用HTML5的占位符属性呢?非常感谢…你帮了我这么多。但是如果我想在我从焦点所在的输入文本移动时不写任何东西,我想输入文本再次获取值password,而不是空的?我该怎么做?我正在尝试使用onchange,但它不起作用…我在我的新功能上面发布了…请您帮助我使用
onblur
而不是
onchange
。因为如果表格一开始是空白的,而你把它留空,它不会改变。非常感谢…你帮了我这么多。但是如果我想在我从焦点所在的输入文本移动时不写任何东西,我想输入文本再次获取值password,而不是空的?我该怎么做?我正在尝试使用onchange,但它不起作用…我在我的新功能上面发布了…请您帮助我使用
onblur
而不是
onchange
。因为如果表格一开始是空白的,而你把它留空,它不会改变。非常感谢…你帮了我这么多。但是如果我想在我从焦点所在的输入文本移动时不写任何东西,我想输入文本再次获取值password,而不是空的?我该怎么做?我正在尝试使用onchange,但它不起作用…我在我的新功能上面发布了…请您帮助我使用
onblur
而不是
onchange
。因为如果表格一开始是空白的,而你把它留空,它不会改变。非常感谢…你帮了我这么多。但是如果我想在我从焦点所在的输入文本移动时不写任何东西,我想输入文本再次获取值password,而不是空的?我该怎么做?我正在尝试使用onchange,但它不起作用…我在我的新功能上面发布了…请您帮助我使用
onblur
而不是
onchange
。因为如果表单最初为空,而您将其保留为空,则它不会更改。
   <input type="text" id="pass_id" name="pass" value="Password" required="required"
            onfocus="changefocus(this)">