0

我使用插件在 javascript 或 Query 中编程两年。

现在我用我的插件打开了这个我的项目,用于扩展文本区域。

我在这句话上有这个新错误(用 Firebug 分析)。类型错误:$.browser 未定义

var hCheck = !($.browser.msie || $.browser.opera);

为什么?

信息

/**
* 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=21, 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();
})
4

3 回答 3

1

您使用的是哪个版本的 jQuery?jQuery.browser已在 jQuery 1.3 中弃用并在 jQuery 1.9 中删除

于 2013-03-04T11:23:30.797 回答
1

$.browser 已从 jQuery 1.9 中删除

暂时使用 jQuery migrate 来修复你的代码:

http://blog.jquery.com/2013/02/16/jquery-migrate-1-1-1-released/

然后使用特征检测重构

于 2013-03-04T11:26:51.760 回答
1

我解决了我的问题,我替换了这段代码:

 (function($) {
    // jQuery plugin definition
    $.fn.TextAreaExpander = function(minHeight, maxHeight) {
      minHeight=21;
    var nAgt=navigator.userAgent;
    var verOffset;
    var hCheck = !(jQuery.support.boxModel || (verOffset=nAgt.indexOf("Opera"))!=-1);
        // var hCheck = !($.browser.msie || $.browser.opera);
      // resize a textarea

在(原图书馆)

(function($) {
// jQuery plugin definition
$.fn.TextAreaExpander = function(minHeight=21, maxHeight) {
var hCheck = !($.browser.msie || $.browser.opera);

这是一个解决方案。

于 2013-04-11T14:34:17.113 回答