Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery/javascript将文本前置到输入字段中_Javascript_Jquery_Placeholder - Fatal编程技术网

jquery/javascript将文本前置到输入字段中

jquery/javascript将文本前置到输入字段中,javascript,jquery,placeholder,Javascript,Jquery,Placeholder,我有一个使用html5占位符的输入框: <input type="text" name="website" class="url" required placeholder="enter website"> 有没有更好的方法来写这篇文章来提高性能等等,这是我真正的问题吗?这比看起来要简单。 我根据@Tahir Yasin的评论制作了一个演示 css: html: 小提琴: 我只是喜欢在动作的每一步都使用jQuery。但正如您所看到的,我的代码的性能不如您的代码,因此您已经在使用性能

我有一个使用html5占位符的输入框:

<input type="text" name="website" class="url" required placeholder="enter website">

有没有更好的方法来写这篇文章来提高性能等等,这是我真正的问题吗?

这比看起来要简单。 我根据@Tahir Yasin的评论制作了一个演示

css:

html:

小提琴:

我只是喜欢在动作的每一步都使用jQuery。但正如您所看到的,我的代码的性能不如您的代码,因此您已经在使用性能最好的方法

以下是基于您的代码的演示:

您可以自己看到差异,几乎相同,但显然您的代码更快。

您想在用户输入前加上“http://”吗?
var input = $("#processor .url");
var prefix = 'http://';

input.focus(function() {
    if (input.val().indexOf(prefix) == -1) {
        input.val(prefix + input.val());
    }
}).blur(function() {
    if (input.val() == prefix) {
        input.val('');
    }
});
.grey { color:#aaa; }
<input type="text" />
var inputval = "";
$(document).ready(function() {
    // Define your default value to show on input
    $("input[type='text']").val("Enter your link here...");
    // Add grey color or optionally blur effect
    $("input[type='text']").addClass('grey');
    // Save your default value
    inputval = $("input[type='text']").val();
    $("input[type='text']").focusin(function() {
    // if textbox is empty or got the default value
    if ($(this).val() == "" || $(this).val() == inputval) {
        // Clear the box
        $(this).val("http://"); // Your text here..
        // Remove grey color
        $(this).removeClass('grey'); }
    }).focusout(function() {
        // if textbox is empty or just contains your value
        if ($(this).val() == "" || $(this).val() == "http://" ) {
            // Put your Default value back
            $(this).val(inputval);
            // Add grey color
            $(this).addClass('grey'); }
    });
});