单击一个按钮以使用“闪烁光标"打开输入字段 [英] Click a button to open a input field with Blinking Cursor

查看:89
本文介绍了单击一个按钮以使用“闪烁光标"打开输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一篇文章中有一个代码:需要有关功能的帮助

I have a Code that I got form my first post :Need help about a function

我修改了这段代码,试图按照自己的意愿进行工作.但是我陷入了困境.

I modified this code and trying to work like how I want .But I got Stuck in one point .

JSfiddle链接: http://jsfiddle.net/saifrahu28/qzKWD/

JSfiddle LINK : http://jsfiddle.net/saifrahu28/qzKWD/

代码在这里:

HTML

<button id="createDiv">Start</button>
<div id="results"></div>

CSS

    #createDiv, #results span { cursor: pointer; }
#results div {
    background: #FFA;
    border: 1px solid;
   width:auto;
}
#results input[type=text] {
    border: none;
    display: none;
    outline: none;
}

JS

    //  Call for document .onload event
$(function() {
    //  Normal Click event asignement, same as $("#createDiv").click(function
    $("#createDiv").on("click", function(e) {
        //  Simply creating the elements one by one to remove confusion
        var newDiv = $("<div />", { class: "new-folder" }),  //  Notice, each child variable is appended to parent
            newInp = $("<input />", { name: "inpTitle[]",style:"display:block ;border:solid 1px #fa9a34", type: "text", value: "Unnamed Group", class: "title-inp" }).appendTo(newDiv),
            newSpan = $("<span />", { id: "myInstance2",style:"display:none", text: "Unnamed Group", class: "title-span" }).appendTo(newDiv);
        //  Everything created and seated, let's append this new div to it's parent
        $("#results").append(newDiv);
    });

    //  the following use the ".delegate" side of .on
    //  This means that ALL future created elements with the same classname, 
    //    inside the same parent will have this same event function added
    $("#results").on("click", ".new-folder .title-span", function(e) {
        //  This hides our span as it was clicked on and shows our trick input, 
        //    also places focus on input
        $(this).hide().prev().show().focus();
    });
    $("#results").on("blur", ".new-folder .title-inp", function(e) {
        //  tells the browser, when user clicks away from input, hide input and show span
        //    also replaces text in span with new text in input
        $(this).hide().next().text($(this).val()).show();
    });
    //  The following sures we get the same functionality from blur on Enter key being pressed
    $("#results").on("keyup", ".new-folder .title-inp", function(e) {
        //  Here we grab the key code for the "Enter" key
        var eKey = e.which || e.keyCode;
        if (eKey == 13) { // if enter key was pressed then hide input, show span, replace text
            $(this).hide().next().text($(this).val()).show();
        }
    });
})

现在要做的是,当我单击开始"按钮时,创建一个可以重命名并另存为文本的输入框.但是我想要的是..当我点击开始按钮时,它会用闪烁光标创建输入,现在不在了.现在,当我单击它时,它变为活动状态.但是我希望它在创建时应该开始闪烁.可以与任何jquery或Java脚本一起使用吗?

WHAT it does now is , when I click the Start Button Create a input Box which Can be Rename and can save as text . BUT what I am wanting is .. WHEN I click the Start button it will Create input With BLINKING cursor which is not here now . Now it become active when I clicked on it . But I am wanting it should Start blinking When it's Created . Is possible with any jquery or java scripts ?

推荐答案

您需要使用 focus() 事件.

You need to use the focus() event.

$("#results").append(newDiv);
newInp.focus();

这篇关于单击一个按钮以使用“闪烁光标"打开输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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