Javascript 如何从插件外部调用JQuery插件内部的函数?
我使用的是textarea弹性插件JQuery。 这就是插件Javascript 如何从插件外部调用JQuery插件内部的函数?,javascript,jquery,plugins,Javascript,Jquery,Plugins,我使用的是textarea弹性插件JQuery。 这就是插件 (function(jQuery){ jQuery.fn.extend({ elastic: function() { // We will create a div clone of the textarea // by copying these attributes from the textarea to the div. var mimics = [
(function(jQuery){
jQuery.fn.extend({
elastic: function() {
// We will create a div clone of the textarea
// by copying these attributes from the textarea to the div.
var mimics = [
'paddingTop',
'paddingRight',
'paddingBottom',
'paddingLeft',
'fontSize',
'lineHeight',
'fontFamily',
'width',
'fontWeight'];
return this.each( function() {
// Elastic only works on textareas
if ( this.type != 'textarea' ) {
return false;
}
var $textarea = jQuery(this),
$twin = jQuery('<div />').css({'position': 'absolute','display':'none','word-wrap':'break-word'}),
lineHeight = parseInt($textarea.css('line-height'),10) || parseInt($textarea.css('font-size'),'10'),
minheight = parseInt($textarea.css('height'),10) || lineHeight*3,
maxheight = parseInt($textarea.css('max-height'),10) || Number.MAX_VALUE,
goalheight = 0,
i = 0;
// Opera returns max-height of -1 if not set
if (maxheight < 0) { maxheight = Number.MAX_VALUE; }
// Append the twin to the DOM
// We are going to meassure the height of this, not the textarea.
$twin.appendTo($textarea.parent());
// Copy the essential styles (mimics) from the textarea to the twin
var i = mimics.length;
while(i--){
$twin.css(mimics[i].toString(),$textarea.css(mimics[i].toString()));
}
// Sets a given height and overflow state on the textarea
function setHeightAndOverflow(height, overflow){
curratedHeight = Math.floor(parseInt(height,10));
if($textarea.height() != curratedHeight){
$textarea.css({'height': curratedHeight + 'px','overflow':overflow});
}
}
// This function will update the height of the textarea if necessary
function update() {
// Get curated content from the textarea.
var textareaContent = $textarea.val().replace(/&/g,'&').replace(/ /g, ' ').replace(/<|>/g, '>').replace(/\n/g, '<br />');
var twinContent = $twin.html();
if(textareaContent+' ' != twinContent){
// Add an extra white space so new rows are added when you are at the end of a row.
$twin.html(textareaContent+' ');
// Change textarea height if twin plus the height of one line differs more than 3 pixel from textarea height
if(Math.abs($twin.height()+lineHeight/3 - $textarea.height()) > 3){
var goalheight = $twin.height()+lineHeight/3;
if(goalheight >= maxheight) {
setHeightAndOverflow(maxheight,'auto');
} else if(goalheight <= minheight) {
setHeightAndOverflow(minheight,'hidden');
} else {
setHeightAndOverflow(goalheight,'hidden');
}
}
}
}
// Hide scrollbars
$textarea.css({'overflow':'hidden'});
// Update textarea size on keyup
$textarea.keyup(function(){ update(); });
$textarea.focus(function(){ update(); });
// And this line is to catch the browser paste event
$textarea.live('input paste',function(e){ setTimeout( update, 250); });
// Run update once when elastic is initialized
update();
});
}
});
(函数(jQuery){
jQuery.fn.extend({
弹性:函数(){
//我们将创建textarea的div克隆
//通过将这些属性从textarea复制到div。
变量模拟=[
“paddingTop”,
“paddingRight”,
"垫底",,
“paddingLeft”,
“字体大小”,
“线宽”,
“丰特家族”,
“宽度”,
“fontWeight”];
返回此值。每个(函数(){
//弹性仅适用于文本区域
if(this.type!=“textarea”){
返回false;
}
var$textarea=jQuery(此),
$twin=jQuery(“”).css({'position':'absolute','display':'none','word-wrap':'break-word'}),
lineHeight=parseInt($textarea.css('line-height'),10)| | parseInt($textarea.css('font-size'),'10'),
minheight=parseInt($textarea.css('height'),10)| | lineHeight*3,
maxheight=parseInt($textarea.css('max-height'),10)| | Number.max_值,
目标高度=0,
i=0;
//如果未设置,Opera将返回最大高度-1
如果(maxheight<0){maxheight=Number.MAX_VALUE;}
//将twin附加到DOM
//我们要测量的是这个区域的高度,而不是文本区域。
$twin.appendTo($textarea.parent());
//将基本样式(模仿)从textarea复制到twin
var i=模拟长度;
而(我--){
$twin.css(模仿[i].toString(),$textarea.css(模仿[i].toString());
}
//在文本区域上设置给定的高度和溢出状态
函数设置高度和溢出(高度,溢出){
curratedHeight=数学楼层(parseInt(高度,10));
如果($textarea.height()!=curratedHeight){
$textarea.css({'height':curratedHeight+'px','overflow':overflow});
}
}
//如有必要,此功能将更新文本区域的高度
函数更新(){
//从文本区域获取策划的内容。
var textareaContent=$textarea.val().replace(//&/g,“&;”).replace(//g,”).replace(//\n/g,“
”);
var twinContent=$twin.html();
if(textareaContent+“”!=twinContent){
//添加额外的空白,以便在行尾添加新行。
$twin.html(textareaContent+“”);
//如果twin加上一行的高度与textarea高度相差超过3像素,则更改textarea高度
if(Math.abs($twin.height()+lineHeight/3-$textarea.height())>3){
变量goalheight=$twin.height()+线宽/3;
如果(目标高度>=最大高度){
设置高度和溢出(最大高度,'auto');
}否则,如果(goalheight查看底部,它将绑定到keyup
和focus
事件:
$textarea.keyup(function(){ update(); });
$textarea.focus(function(){ update(); });
//this should be just $textarea.keyup(update); but that's a another question :)
您可以通过为这些事件处理程序中的任何一个使用来触发update()
函数,如下所示:
$('textarea').triggerHandler('keyup');