让用户在网页上创建更多元素

时间:2013-06-29 04:43:56

标签: javascript html5 user-input

我想知道是否有用户添加到html5网页的元素?例如,他们是否可以将段落或图像添加到以前不存在的现有网页中?

例如

<!doctype html>
    <html>
    <head>
    </head>
    <body>
    <p class=”pClass”>This is in a paragraph.</p>

    **//would it be possible for a user to insert several paragraphs here with the click of a button or something?**


    </body>
    </html>

3 个答案:

答案 0 :(得分:2)

我可能做得太过分了,但我总是喜欢创造这些小例子。这就是我想出来的。从这个HTML开始:

<input type="text" id="txt1" />
<button id="btn1">Add Item</button>
<hr />
<div id="targetArea"></div>

这个CSS:

p.editable input.editor,
p.editable input.action,
p.editable em.escape {
    display: none;
}

p.editable input.action {
    margin-left: 15px;
}

p.editable em.escape {
    margin-left: 10px;
    font-size: 8px;
}

p.editable:hover input.action {
    display: inline;
}

p.editable.editing span.text {
    display: none !important;
}

p.editable.editing input.editor,
p.editable.editing input.action,
p.editable.editing em.escape {
    display: inline !important;
}

并使用此JavaScript:

var textProp = "textContent" in document.createElement("div") ? "textContent" : "innerText";

function strTrim(str) {
    if (!str) {
        str = "";
    }
    return str.replace(/^\s+|\s+$/g, "");
}

function addEvent(element, eventName, callback) {
    if (element.addEventListener) {
        element.addEventListener(eventName, callback, false);   
    } else if (element.attachEvent) {
        element.attachEvent("on" + eventName, callback);
    }
}

function editableClickHandler(e) {
    var actionButton, pNode, myText, myEditor;

    actionButton = this;
    pNode = actionButton.parentNode;
    myText = pNode.querySelector("span.text");
    myEditor = pNode.querySelector("input.editor");

    if (actionButton.value === "Edit") {
        actionButton.value = "Done";
        pNode.className += " editing";
        myEditor.setAttribute("data-original-val", myText[textProp]);    // Save current value in case of canceling
        myEditor.value = myText[textProp];
    } else {
        actionButton.value = "Edit";
        pNode.className = "editable";
        myText[textProp] = myEditor.value;
    }
}

function escapeCheckHandler(e) {
    var keyCode, pNode, myEditor, myText, myActionButton;

    e = e || window.event;    // Normalize event
    keyCode = e.keyCode || e.which || e.charCode;    // Normalize keycode

    if (keyCode === 27) {    // Escape key
        pNode = this.parentNode;
        myEditor = pNode.querySelector("input.editor");
        myText = pNode.querySelector("span.text");
        myActionButton = pNode.querySelector("input.action");

        pNode.className = "editable";
        myText = myEditor.getAttribute("data-original-val");
        myActionButton.value = "Edit";
    }
}

function addClickHandler() {
    var target, curInput, curInputVal, newP, newText, newEditor, newActionButton, newEscapeInfo;

    curInput = document.getElementById("txt1");
    curInputVal = strTrim(curInput.value);

    if (!curInputVal) {
        alert("Must provide actual text");
        return;
    }

    target = document.getElementById("targetArea");
    newP = document.createElement("p");
    newText = document.createElement("span");
    newEditor = document.createElement("input");
    newActionButton = document.createElement("input");

    newP.className = "editable";

    newText.className = "text";
    newText[textProp] = curInputVal;
    newP.appendChild(newText);

    newEditor = document.createElement("input");
    newEditor.type = "text";
    newEditor.className = "editor";
    addEvent(newEditor, "keyup", escapeCheckHandler);
    newP.appendChild(newEditor);

    newActionButton.type = "button";
    newActionButton.className = "action";
    newActionButton.value = "Edit";
    addEvent(newActionButton, "click", editableClickHandler);
    newP.appendChild(newActionButton);

    newEscapeInfo = document.createElement("em");
    newEscapeInfo.className = "escape";
    newEscapeInfo[textProp] = "(Press Escape to Cancel Editing)";
    newP.appendChild(newEscapeInfo);

    curInput.value = "";
    target.insertBefore(newP, target.firstChild);
}

function loadHandler() {
    addEvent(document.getElementById("btn1"), "click", addClickHandler);
}

addEvent(window, "load", loadHandler);

DEMO: http://jsfiddle.net/8K3pN/2/

使用addEvent有助于跨浏览器更一致地绑定事件处理程序。

总体而言,当您填写文本框并单击按钮时,它会向目标区域添加<p>(带有一些子元素)。根据{{​​1}}的状态,隐藏/显示某些内容(使用CSS)。并且根据按钮的状态单击(编辑/完成),会发生某些事情。我添加了能够按Escape键“取消”编辑(而不是添加另一个按钮)。

同样,我知道这可能太多了,但我希望这有助于您了解这一切如何协同工作!

答案 1 :(得分:0)

正如其他人所说,我们不确定您想要完成什么,但您可以使用document.createElement()函数从JavaScript向文档中添加元素。您可以提供一个文本框来输入文本,然后调用JS函数添加一个段落到您的div。

答案 2 :(得分:0)

这是你的想法吗?

<!doctype html>
    <html>
    <head>
    <script type="text/javascript">
        var count = 1;
        function newPara() {
            var new_p = document.createElement('p');
            new_p.setAttribute('class','pClass');
            new_p.setAttribute('id','new_para_'+ count++);

            var newContent = document.createTextNode(document.getElementById('content').value);
            new_p.appendChild(newContent);

            document.getElementsByTagName('body')[0].insertBefore(new_p,document.getElementById('create'));
        }
    </script>
    </head>
    <body>
    <p class=”pClass”>This is in a paragraph. <br/></p>


    <div id="create">
    <textarea id="content"> </textarea>
    <button onclick="newPara();"> Create </button>
    </div>


    </body>
    </html>