ama*_*eur 5 jquery textbox multiline
我有一个单行文本框.
我想用jquery将它转换为多行但控制添加到它的行数.我也希望能够限制每行的字符数.
我有什么想法可以用jquery做到这一点?
编辑:
是的,我的意思是文本框是 <input type="text">
例如. <input type="text" value="" name="txtTextBox1" id="xtTextBox1">
考虑到您有以下 HTML:
<input type="text" class="myclasses" style="color: #123123" value="akira"/>
Run Code Online (Sandbox Code Playgroud)
然后使用以下代码片段:
(function($) {
$.fn.changeToTextArea = function(rows, columns) {
var attrs = {};
var text = "";
$.each(this[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
if(attr.nodeName == "value") {
text = attr.nodeValue;
}
attrs["rows"] = rows;
attrs["cols"] = columns;
});
this.replaceWith(function() {
return $("<textarea/>", attrs).append($(this).contents()).html(text);
});
}
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
你应该用
$("input").changeToTextArea(7, 25);
Run Code Online (Sandbox Code Playgroud)
<input type="text" class="myclasses" style="color: #123123" value="akira"/>
Run Code Online (Sandbox Code Playgroud)
(function($) {
$.fn.changeToTextArea = function(rows, columns) {
var attrs = {};
var text = "";
$.each(this[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
if(attr.nodeName == "value") {
text = attr.nodeValue;
}
attrs["rows"] = rows;
attrs["cols"] = columns;
});
this.replaceWith(function() {
return $("<textarea/>", attrs).append($(this).contents()).html(text);
});
}
})(jQuery);
Run Code Online (Sandbox Code Playgroud)