Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅使用代码在div内创建工作表单_Javascript_Jquery - Fatal编程技术网

Javascript 仅使用代码在div内创建工作表单

Javascript 仅使用代码在div内创建工作表单,javascript,jquery,Javascript,Jquery,我有一个部门: <div id="test">Text to be removed</div> 要删除的文本 如何仅使用代码将此完整表单添加到div中(从零开始,意味着表单元素在页面中的任何位置都不存在): 编辑: 那么 $('#test').html('<form name="input" action="html_form_action.asp" method="get">' + 'Edit: <inpu

我有一个部门:

<div id="test">Text to be removed</div>
要删除的文本
如何仅使用代码将此完整表单添加到div中(从零开始,意味着表单元素在页面中的任何位置都不存在):


编辑:
那么

$('#test').html('<form name="input" action="html_form_action.asp" method="get">' +
                    'Edit: <input type="text" name="tex" value="Some text here to edit"/>' +
                    '<input type="submit" value="Submit" />' +
                 '</form>');

或者使用本机API和辅助函数

function createAndInsert(parent, tag, props, clear_first) {
   var el;

    if(typeof parent === 'string')
        parent = document.getElementById(parent);

    if(clear_first)
        parent.innerHTML = '';

    if(tag === ':text')
        el = parent.appendChild(document.createTextNode(props));
    else {
        el = parent.appendChild(document.createElement(tag));

        for(var n in props)
            el[n] = props[n];
    }
    return el;
}
然后

var form = createAndInsert('test', 'form', {
   name: 'input',
   action: 'html_form_action.asp',
   method: 'get'
}, true);

createAndInsert(form, ':text', 'Edit: ');

createAndInsert(form, 'input', {type:'text',name:'tex',value:'Some text here to edit'});

createAndInsert(form, 'input', {type:'submit',value:'Submit'});
那么

$('#test').html('<form name="input" action="html_form_action.asp" method="get">' +
                    'Edit: <input type="text" name="tex" value="Some text here to edit"/>' +
                    '<input type="submit" value="Submit" />' +
                 '</form>');

或者使用本机API和辅助函数

function createAndInsert(parent, tag, props, clear_first) {
   var el;

    if(typeof parent === 'string')
        parent = document.getElementById(parent);

    if(clear_first)
        parent.innerHTML = '';

    if(tag === ':text')
        el = parent.appendChild(document.createTextNode(props));
    else {
        el = parent.appendChild(document.createElement(tag));

        for(var n in props)
            el[n] = props[n];
    }
    return el;
}
然后

var form = createAndInsert('test', 'form', {
   name: 'input',
   action: 'html_form_action.asp',
   method: 'get'
}, true);

createAndInsert(form, ':text', 'Edit: ');

createAndInsert(form, 'input', {type:'text',name:'tex',value:'Some text here to edit'});

createAndInsert(form, 'input', {type:'submit',value:'Submit'});

我喜欢第二个例子。你能修改它以生成OP要求的HTML吗?它在输入文本之前省略了
Edit:
literal.+1,很好的例子。很好地展示了操作DOM的不同方法。谢谢@DarinDimitrov:是的,我一开始错过了文本节点。另外,刚刚为DOMAPI版本添加了一个帮助函数,使其更加美观。:)@David19801:这是用于操作页面元素的实际API。有些人觉得使用起来很笨拙,所以他们使用了类似jQuery的库,它只不过是API上的一层而已。最终,domapi是jQuery用来操作页面的工具。我从这个大答案中学到了很多东西。非常感谢。我喜欢第二个例子。你能修改它以生成OP要求的HTML吗?它在输入文本之前省略了
Edit:
literal.+1,很好的例子。很好地展示了操作DOM的不同方法。谢谢@DarinDimitrov:是的,我一开始错过了文本节点。另外,刚刚为DOMAPI版本添加了一个帮助函数,使其更加美观。:)@David19801:这是用于操作页面元素的实际API。有些人觉得使用起来很笨拙,所以他们使用了类似jQuery的库,它只不过是API上的一层而已。最终,domapi是jQuery用来操作页面的工具。我从这个大答案中学到了很多东西。非常感谢。