使用javascript将1 div内容复制到另一个页面div

时间:2015-10-16 17:04:45

标签: javascript

你好我们在运行时开发一个表单并在div中附加元素。

但是当将div的内容复制到另一个页面时,它没有显示任何内容,好像没有附加任何内容。你能告诉我这是什么问题......

https://jsfiddle.net/a80xyep8

<form>

    <div class="dynamicInput" id="dynamicInput">
        /*all dynamic elements are appended here as per javascript coding . but not showing in another page only static things are showing shuch as
        <input type="text" name="a" /> */
    </div>
    <input type="submit" value="submit_form" onclick="f();" />
</form>

<script>
    var counterText = 0;
    var counterRadioButton = 0;
    var counterCheckBox = 0;
    var counterTextArea = 0;
    var lb = "";
    var name = "";

    function addAllInputs(divName, itemName) {

        var newdiv = document.createElement('div');
        switch (itemName) {
            case 'text':
                lb = prompt("enter label name");
                name = prompt("enter name for div attribute");
                //newdiv.innerHTML = " "+(counterText + 1)+ " "+ lb + " <br><input type='text' name='myInputs[]'>";
                newdiv.innerHTML = " " + lb + " <br><input type='text'>";
                newdiv.setAttribute("id", name);
                newdiv.setAttribute("name", name);
                counterText++;
                document.getElementById(divName).appendChild(newdiv);
                break;
            case 'radio':
                {
                    var n = prompt("How many number of elements do you need");
                    lb = prompt("enter label name");

                    newdiv.innerHTML = " " + lb + "<br>";
                    document.getElementById(divName).appendChild(newdiv);
                    for (var i = 0; i < n; i++) {
                        var newdiv1 = document.createElement('div');
                        lb = prompt("enter label name");
                        name = prompt("enter name for div attribute");
                        newdiv1.innerHTML = " " + lb + "<input type='radio'>";
                        newdiv1.setAttribute("id", name);
                        newdiv1.setAttribute("name", name);
                        counterRadioButton++;
                        document.getElementById(divName).appendChild(newdiv1);
                    }
                    counterRadioButton++;
                    break;
                }

            case 'checkbox':
                var n = prompt("How many number of elements do you need");
                if (n == 1) {
                    lb = prompt("enter label name");
                    name = prompt("enter name for div attribute");
                    newdiv.innerHTML = " " + lb + " <br><input type='checkbox' >";
                    newdiv.setAttribute("id", name);
                    newdiv.setAttribute("name", name);
                    document.getElementById(divName).appendChild(newdiv);
                    counterCheckBox++;
                } else if (n > 1) {
                    var newdiv1 = document.createElement('div');
                    lb = prompt("enter label name");
                    newdiv1.innerHTML = " " + lb;
                    document.getElementById(divName).appendChild(newdiv1);
                    for (var i = 0; i < n; i++) {
                        var newdiv1 = document.createElement('div');
                        lb = prompt("enter label name");
                        name = prompt("enter name for div attribute");
                        newdiv1.innerHTML = " " + lb + " <br><input type='checkbox'>";
                        newdiv.setAttribute("id", name);
                        newdiv.setAttribute("name", name);
                        document.getElementById(divName).appendChild(newdiv1);
                        counterCheckBox++;
                    }
                }

                break;

            case 'textarea':
                lb = prompt("enter label name");
                name = prompt("enter name for div attribute");
                newdiv.innerHTML = " " + lb + " <br><textarea>type here...</textarea>";
                newdiv.setAttribute("id", name);
                newdiv.setAttribute("name", name);
                counterTextArea++;
                document.getElementById(divName).appendChild(newdiv);
                break;


                //document.getElementById(divName).appendChild(newdiv);
            case 'combobox':
                lb = prompt("enter label name");
                name = prompt("enter name for div attribute");
                newdiv.innerHTML = " " + lb + " <br><select id='combo[]'>type here...</select>";
                document.getElementById(divName).appendChild(newdiv);
                var textb = lb;
                var combo = document.getElementById("combo");
                newdiv.setAttribute("id", name);
                newdiv.setAttribute("name", name);
                var option = document.createElement("option");
                option.text = textb.value;
                option.value = textb.value;
                try {
                    combo.add(option, null); //Standard 
                } catch (error) {
                    combo.add(option); // IE only
                }
                textb.value = "";
                document.getElementById(divName).appendChild(newdiv);
        }
    }
</script>

1 个答案:

答案 0 :(得分:0)

我认为你应该等待DOM准备好了:

<script>
    $( document ).ready( function() {
        document.getElementById("copy").innerHTML=sessionStorage.getItem("page1content");
    } )
</script>

以这种方式使用你的代码对我来说非常有用。