jQuery将光标移动到文本输入的末尾 [英] jQuery move cursor to end of text input

查看:143
本文介绍了jQuery将光标移动到文本输入的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有一个输入框,其值为当前网站"

Currently I have an input box which has the value "Current Website"

当他们单击它时,我将运行此功能:

When they click it I run this function:

function clearMeHttp(formfield) {
    if (formfield.defaultValue == formfield.value) {
        formfield.value = "http://";
    }
};

如果您单击该框,它会很好地工作,但是如果您单击该框,它只会突出显示单词"http://",这会破坏其目的,除非他们按了我要避免的向右箭头键.

It works fine if you click into the box, but if you tab into the box it just highlights the word "http://" which defeats the purpose of it unless they hit the right arrow key which I want to avoid.

谢谢!

这是其他代码:<input onblur="restoreMe(this)" onfocus="clearMeHttp(this)" type="text" class="fade" name="website" value="Current website">

restoreMe只是使用jquery将默认值逐渐变小

restoreMe just fades default value back in gently with jquery

推荐答案

我对您的代码做了一些更改,但是使用了jquery事件而不是内联html.

I have changed your code a bit, but uses jquery events instead of inline html.

已使用完整脚本:

var defaultVal = 'Current website';
$(function() {

    $('.urlCheck').focus(function(e) {
        if ($(this).val() == "http://" || $(this).val() == '' || $(this).val() == defaultVal) {
            $(this).val('http://');
            $(this).select(false);

        }
    });

    $('.urlCheck').keyup(function(e) {
        if ($(this).val() == "http://" || $(this).val() == '' || $(this).val() == defaultVal) {
            $(this).val('http://');
        }
    });

    $('.urlCheck').blur(function(e) {
        $(this).val(defaultVal);
    });

});

工作链接: http://jsfiddle.net/29gks/6/

keyup 事件是Chrome和Safari的替代项

The keyup event is the override for Chrome and Safari

这篇关于jQuery将光标移动到文本输入的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆