cwd*_*cwd 5 javascript jquery attributes dom properties
我有一个文本输入,我想用textarea替换,保留所有属性.我怎么能用jQuery做到这一点?
样本输入:
<input type="text" name="newfeature" id="newfeature"
class="form-required" tabindex="3"
aria-required="true" />
Run Code Online (Sandbox Code Playgroud)
期望的输出:
<textarea type="text" name="newfeature" id="newfeature"
class="form-required" tabindex="3"
aria-required="true"></textarea>
Run Code Online (Sandbox Code Playgroud)
我知道这可以使用.prop()
选择器手动完成并指定每个属性/属性,但我该如何动态地完成?
此外,这不是必需的,但在执行此操作时是否可以保留绑定?
这也可以正确地使用该value
属性:
$(\'input\').each(function() {\n\n var attrs = {}; \n\n $.each(this.attributes, function() {\n attrs[this.name] = this.value;\n });\n\n $(this).replaceWith($(\'<textarea>\').prop(attrs));\n});\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n示例: http: //jsfiddle.net/FyYDT/
\n\n添加
\n\n如果您希望 jQuery 事件传递(不推荐),您需要重新绑定它们。就像是:
\n\n$(\'input\').click(function() {\n\n alert(\'hey\');\n\n}).each(function() {\n\n var attrs = {},\n ta = $(\'<textarea>\');\n\n $.each(this.attributes, function() {\n attrs[this.name] = this.value;\n });\n\n $.each($(this).data(\'events\'), function(i, f) {\n $.each(f, function(){\n ta.bind(i, this.handler);\n });\n });\n\n $(this).replaceWith(ta.prop(attrs));\n\n});\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n\n