如何使用jquery 将输入元素更改为textarea
我有以下html:
现在我想将大小为’100’的每个输入更改为与输入具有相同样式的textarea。
我试过这个:
$("input[size=100]").each(function(){ //how to replace it? });
有任何想法吗?
我在这里的答案中使用了这个解决方案:
$("input[size=100]").each(function() { var style = $(this).attr('style'), textbox = $(document.createElement('textarea')).attr({ id : $(this).id, name : $(this).name, value : $(this).val(), style : $(this).attr("style"), "class" : $(this).attr("class"), rows : 6 }).width($(this).width()); $(this).replaceWith(textbox); });
jQuery有一个.replaceWith()
方法,你可以用它来替换另一个元素。 因此,从输入中复制样式并使用此方法,如下所示:
$('input[size="100"]').each(function () { var style = $(this).attr('style'), textbox = $(document.createElement('textarea')).attr('style', style); $(this).replaceWith(textbox); });
演示
尝试一些类似的东西
$('input[size="100"]').each(function() { var textarea = $(document.createElement('textarea')); textarea.text($(this).val()); $(this).after(textarea).remove(); });
这是一个例子: http : //jsfiddle.net/SSwhK/
试试这样吧
$("input").each(function(){ if($(this).attr('size') == "100") { my_style = $(this).attr('style'); $(this).replaceWith(""); } });
$(document).ready(function(){ $("input[size=100]").each(function(){ //how to replace it? $(this).after('').remove(); }); });
查看http://jsfiddle.net/6JH6B/