选择所有焦点文字

时间:2019-07-13 02:35:20

标签: javascript

我正在创建云服务器客户端的前端。我想添加一个可能性来创建一个新文件夹。因此,当我单击“新建文件夹”按钮时,我正在创建并添加一个un元素,它将是新文件夹。我将其命名为“ contenteditable”,并将其放在焦点上,以便用户可以更改新文件夹的名称。我找不到方法是当元素处于焦点时,自动选择所有文本。有谁知道该怎么做?

我尝试使用.select(),但是,或者它无法识别该函数,或者它说元素未定义。

这是我的ajax函数的成功之处:

        var results = JSON.parse(data);
        var path = results[0];
        var idFolder = results[1];
        var nameFolder = results[2]

        // Create a card of the new Folder
        var column = document.createElement("div");
        column.setAttribute("class", "column");
        column.setAttribute("name", path); // We use it to add or delete element to avoid refresh. We dont't have it for now in Google Drive

        var container = document.createElement("div");
        container.setAttribute("class", "container");

        var card = document.createElement("div");
        card.setAttribute("class", "card");
        card.setAttribute("id", path);
        card.setAttribute("data-serviceid", serviceIdDropBox);
        card.setAttribute("data-serviceemail", serviceEmailDropBox);
        card.setAttribute("data-size", null);
        card.setAttribute("title", nameFolder);

        var image = document.createElement("img");
        image.setAttribute("src", "/icons/iconfinder_folder.png");
        image.setAttribute("onclick", "loadDataDropbox('" + serviceIdDropBox + "','" + serviceEmailDropBox + "','" + path + "')");

        var name = document.createElement("div");
        name.setAttribute("title", nameFolder);
        name.setAttribute("data-toggle", "tooltip");
        name.setAttribute("data-placement", "bottom");
        name.setAttribute("id", idFolder);
        name.setAttribute("onclick", "Rename('" + idFolder + "', '" + path + "')");
        name.style.wordWrap = "break-word";
        name.style.width = "100px";
        name.innerText = nameFolder.substring(0, 10);


        var row = document.getElementById("allData");

        // append the elements
        card.appendChild(image);
        card.appendChild(name);
        container.appendChild(card);
        column.appendChild(container);

        row.insertBefore(column, row.children[0]); // or row.firstChild

        var contentWindow = document.getElementById("contentWindow");

        // append "allData" div to the column
        contentWindow.appendChild(row);



        renameNodeNewFolder = document.getElementById(idFolder);

        renameNodeNewFolder.setAttribute("contenteditable", true);
        renameNodeNewFolder.focus();
        renameNode = renameNodeNewFolder;

在创建新文件夹时,我想让它的默认名称成为焦点,选中并准备进行编辑。

0 个答案:

没有答案