duan5731
2016-07-09 21:04 阅读 64
已采纳

在PHP中添加onfocus和onblur

I have tried inputting the onfocus and onblue event within a form within PHP on a WordPress website, and cannot fathom the best way to do so:

Here is the form code within 'general'template.php' which is a WordPress file:

$form = '
    <form name="' . $args['form_id'] . '" id="' . $args['form_id'] . '" action="' . esc_url( site_url( 'wp-login.php', 'login_post' ) ) . '" method="post">
        ' . $login_form_top . '
            <input type="text" name="log" id="' . esc_attr( $args['id_username'] ) . '" class="input" value="Username" size="20"  />
            <input type="password" name="pwd" id="' . esc_attr( $args['id_password'] ) . '" class="input" value="Password" size="20" />
        ' . $login_form_middle . '
        ' . ( $args['remember'] ? '<label><input name="rememberme" type="checkbox" id="' . esc_attr( $args['id_remember'] ) . '" value="forever"' . ( $args['value_remember'] ? ' checked="checked"' : '' ) . ' /> ' . esc_html( $args['label_remember'] ) . '</label>' : '' ) . '
            <input type="submit" name="wp-submit" id="' . esc_attr( $args['id_submit'] ) . '" class="button-primary" value="' . esc_attr( $args['label_log_in'] ) . '" />
            <input type="hidden" name="redirect_to" value="' . esc_url( $args['redirect'] ) . '" />
            <a class="btn btn-theme" href="/register/">Register</a>
        ' . $login_form_bottom . '
    </form>';

I want to add the onfocus and onblur events to the Username and Password fields.

 onfocus="if(this.value  == 'Username') { this.value = ''; } " onblur="if(this.value == '') { this.value = 'Username'; } "

What would be the best way to go about this?

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doufan1899 doufan1899 2016-07-09 21:33

    You need to escape the single quotes using backslash. So, you need to use this way:

    onfocus="if(this.value  == \'Username\') { this.value = \'\'; } " onblur="if(this.value == \'\') { this.value = \'Username\'; } "
    
    点赞 评论 复制链接分享

相关推荐