javascript - 需要有关函数的帮助

标签 javascript jquery html css

我是 Javascript 和 jQuery 新手。我需要以下方面的帮助:

<script type="text/javascript">
    $(window).load(function(){
        $('#createDiv').click(function (){
            $("<div/>").html("<span id='myInstance2' style='display: block;'>New Folder</span>").css("display", "none").appendTo("#results").fadeIn();
        });
    });
</script>

我有这个功能可以点击并创建一个新的<span>带有文本的标签。你可以看到我有一个ID myInstance2 。所以我想做的是,当我单击并创建跨度时,我想让这个跨度可实时编辑。就像我可以将这个“新文件夹”重命名为我想要的任何名称。

如果有人能提供帮助那就太好了。提前致谢,并对我的英语不好表示歉意:)

最佳答案

如果我明白你想要做的事情,那它并不像你想象的那样可行。不过,也有窍门。以下是其中之一。这个想法是在跨度所在的位置插入一个“隐藏”输入,然后创建函数来显示输入并在需要时隐藏跨度(例如当用户单击跨度时)。如下所示:

jsFiddle

HTML

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

CSS

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

JavaScript

//  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 />", { type: "text", value: "New Folder", class: "title-inp" }).appendTo(newDiv),
            newSpan = $("<span />", { id: "myInstance2", text: "New Folder", 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();
        }
    });
})

关于javascript - 需要有关函数的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16401965/

相关文章:

javascript - 处理postJSON的结果

jquery - 使用 jQuery 在 CSS 中设置水平滚动条样式

html - 如何在 CSS 垂直条形图中底部对齐条形图?

javascript - 是否可以在 ioredis 中使用单个 .sadd/.zadd 调用执行多个 SADD 或 ZADD 分配?

javascript - 如何获取与 angularjs 的 ui-router 一起使用的多个 View 的示例

c# - Kendo UI 上传不返回 onSuccess

javascript - If-else 语句的短路

Javascript:动态创建按钮的onclick

jQuery:在表单提交时,更改表单的样式类

html - 如何在最大宽度内容 div 后面拉伸(stretch)背景图像