【问题标题】:I created textarea expander from script but after, it doesn't expands我从脚本创建了 textarea 扩展器,但之后它没有扩展
【发布时间】:2012-10-19 01:34:09
【问题描述】:

我不知道标题是否正确。 我用一个脚本创建了一个表,其中包含 5 个文本区域和 class="expand"。 我写作时这个文本区域会扩展但随后不起作用。 有没有写完后调用jquery插件的方法? 在我尝试不创建 textarea 之前,我在 html 文件中编写了它并且它有效。

(关于 2 textarea 的信息和示例:

http://blogs.sitepointstatic.com/examples/tech/textarea-expander/index.html
http://jsfiddle.net/V2RLs/2/ (without class="expand")

) 但是当我在 textarea 中写入后,它并没有扩展。为什么?

这是我的脚本:

<script src='js/jquery.textarea-expander.js'></script>
<script >$(document).ready(function() { 
var regex,v,l,c,b;

$( "#wnd_Addparam" ).dialog({
            autoOpen: false,
            height: 'auto',
            width: 350,
            modal: true,
            resizable:false,
            buttons: {
                "Add": function() {
                                 contapara=(parseInt(contapara)+1);

                document.getElementById("sorpara").innerHTML+="<li id=\"inputp"+contapara+"_id\" class=\"ui-state-default\"><span class=\"ui-icon ui-icon-arrowthick-2-n-s\"></span>"+txt_idparam.value+"</li>";    
                $('#formp').submit();
                                $( this ).dialog( "close" ); 
                                   },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            },
            close: function() {
                $( this ).dialog( "close" );
            }
        });

        $( "#btn_Addpar" ).click(function() {
                        i=(parseInt(contapara)+1);

    $('#wnd_Addparam').append('<form method="GET" id="formparam"><table><tr><td><label> ID  </label></td><td><textarea class="expand" id="inputp'+i+'_id" class="text"> </textarea></td></tr><tr><td><label>Type</label></td><td><select id="inputp'+i+'_type"><option value="text">Text</option><option value="integer">Integer</option><option value="float">Float</option><option value="list_values">List of values</option><option value="range">Range</option><option value="selection_collapsed">Selection (collapsed)</option><option value="selection_expanded">Selection (expanded)</option><option value="subimage">Subimage selection</option><option value="polygon">Polygon selection</option><option value="horizontal_separator">Horizontal separator</option></select></td></tr><tr><td> <label > Description</label></td> <td><textarea class="expand" id="inputp'+i+'_description" size=24></textarea></td></tr><tr><td><label>Value</label></td><td><textarea class="expand" id="inputp'+i+'_value"></textarea></td></tr><tr><td><label>Info (help)</label></td><td><textarea class="expand" id="inputp'+i+'_info"></textarea></td></tr><tr><td><label> Visible?</label></td><td><input type="checkbox" id="inputp'+i+'_visible"></td></tr></table>/form>');
                $( "#wnd_Addparam" ).dialog( "open" );
            });});</script>

插件扩展器..

/**
* TextAreaExpander plugin for jQuery
* v1.0
* Expands or contracts a textarea height depending on the
* quatity of content entered by the user in the box.
*
* By Craig Buckler, Optimalworks.net
*
* As featured on SitePoint.com:
* http://www.sitepoint.com/blogs/2009/07/29/build-auto-expanding-textarea-1/
*
* Please use as you wish at your own risk.
*/
/**
* Usage:
*
* From JavaScript, use:
* $(<node>).TextAreaExpander(<minHeight>, <maxHeight>);
* where:
* <node> is the DOM node selector, e.g. "textarea"
* <minHeight> is the minimum textarea height in pixels (optional)
* <maxHeight> is the maximum textarea height in pixels (optional)
*
* Alternatively, in you HTML:
* Assign a class of "expand" to any <textarea> tag.
* e.g. <textarea name="textarea1" rows="3" cols="40" class="expand"></textarea>
*
* Or assign a class of "expandMIN-MAX" to set the <textarea> minimum and maximum height.
* e.g. <textarea name="textarea1" rows="3" cols="40" class="expand50-200"></textarea>
* The textarea will use an appropriate height between 50 and 200 pixels.
*/
(function($) {
// jQuery plugin definition
$.fn.TextAreaExpander = function(minHeight, maxHeight) {
var hCheck = !($.browser.msie || $.browser.opera);
// resize a textarea
function ResizeTextarea(e) {
// event or initialize element?
e = e.target || e;
// find content length and box width
var vlen = e.value.length, ewidth = e.offsetWidth;
if (vlen != e.valLength || ewidth != e.boxWidth) {
if (hCheck && (vlen < e.valLength || ewidth != e.boxWidth)) e.style.height = "0px";
var h = Math.max(e.expandMin, Math.min(e.scrollHeight, e.expandMax))+2;
e.style.overflow = (e.scrollHeight > h ? "auto" : "hidden");
e.style.height = h + "px";
e.valLength = vlen;
e.boxWidth = ewidth;
}
return true;
};
// initialize
this.each(function() {
// is a textarea?
if (this.nodeName.toLowerCase() != "textarea") return;
// set height restrictions
var p = this.className.match(/expand(\d+)\-*(\d+)*/i);
this.expandMin = minHeight || (p ? parseInt('0'+p[1], 10) : 0);
this.expandMax = maxHeight || (p ? parseInt('0'+p[2], 10) : 99999);
// initial resize
ResizeTextarea(this);
// zero vertical padding and add events
if (!this.Initialized) {
this.Initialized = true;
$(this).css("padding-top", 0).css("padding-bottom", 0);
$(this).bind("keyup", ResizeTextarea).bind("focus", ResizeTextarea);
}
});
return this;
};
})(jQuery);
// initialize all expanding textareas
jQuery(document).ready(function() {
jQuery("textarea[class*=expand]").TextAreaExpander();
});

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-plugins textarea


    【解决方案1】:

    插件会在 document.ready 上准备 textarea,因此在 document.ready 触发后该类分配给的任何 textarea 都不会受到插件的影响 - 但是,您可以直接调用插件而不是添加类:

    jQuery(".expand").TextAreaExpander();
    

    但是,您不应该在一个元素上运行 TextAreaExpander 两次,因此如果您有使用在 document.ready 上初始化的插件的 textareas,请确保为新生成的 textareas 使用另一个类名 - 或者您可以删除最后几行插件。

    【讨论】:

    • 这个标题的问题对吗?在我删除最后几行之前,但之后不工作。我在 appendTo 这句话之后写: jQuery("textarea[class*=expand]").TextAreaExpander();文本区域工作但变得非常小。我通过添加插件来编辑问题。
    • 插件的默认设置是将文本区域缩小到可能的最小尺寸 - 但是,TextAreaExpander 支持设置文本区域的最大和最小垂直尺寸的参数。 $.fn.TextAreaExpander = function(minHeight, maxHeight)
    • 请问Damon,您的回答是正确且有趣的。但是这个变量是哪里来的?
    • 这是一个函数定义 - jQuery(".expand").TextAreaExpander(5,10); 将创建一个最小高度为 5 且最大为 10 的文本区域。
    • $('#wnd_Addparam').find('textarea').TextAreaExpander();这是调用新文本区域创建的语句
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2014-02-15
    相关资源
    最近更新 更多