Skip to content

Commit

Permalink
优化事件绑定
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 8, 2015
1 parent 1dfdf3a commit 2d5cfd6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
29 changes: 13 additions & 16 deletions build/MDEditor.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,12 +799,15 @@
(function(window, undefined) {
var _$ = document.querySelectorAll.bind(document);
function addEvent(object, event, method) {
if (object.addEventListener) {
object.addEventListener(event, method, false);
} else if (object.attachEvent) {
object.attachEvent("on" + event, function() {
method(window.event);
});
event = event.split(" ");
for (var i = 0; i < event.length; i++) {
if (object.addEventListener) {
object.addEventListener(event[i], method, false);
} else if (object.attachEvent) {
object.attachEvent("on" + event[i], function() {
method(window.event);
});
}
}
}
function _applyAttrs(context, attrs) {
Expand Down Expand Up @@ -913,10 +916,10 @@
css(self.preview, {
height: self.textarea.offsetHeight + "px"
});
valueChange = function(obj, event) {
if (obj.scrollHeight < self.opts.maxheight) {
addEvent(self.textarea, "input propertychange", function(event) {
if (this.scrollHeight < self.opts.maxheight) {
css(self.textarea, {
height: obj.scrollHeight + "px",
height: this.scrollHeight + "px",
overflow: "hidden"
});
} else {
Expand All @@ -928,14 +931,8 @@
css(self.preview, {
height: self.textarea.offsetHeight + "px"
});
self.opts.value = obj.value;
self.opts.value = this.value;
self.callback(event, self.opts);
};
addEvent(self.textarea, "input", function(event) {
valueChange(this, event);
});
addEvent(self.textarea, "propertychange", function(event) {
valueChange(this, event);
});
return this;
};
Expand Down
28 changes: 12 additions & 16 deletions lib/MDEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
* @param {[type]} method [执行函数]
*/
function addEvent(object, event, method) {
if (object.addEventListener){
object.addEventListener(event, method, false);
}else if(object.attachEvent){
object.attachEvent('on'+event, function(){ method(window.event); });
event = event.split(' ')
for(var i = 0;i<event.length;i++){
if (object.addEventListener){
object.addEventListener(event[i], method, false);
}else if(object.attachEvent){
object.attachEvent('on'+event[i], function(){ method(window.event); });
}
}
}

Expand Down Expand Up @@ -170,10 +173,11 @@

css(self.preview,{'height':self.textarea.offsetHeight + "px"});

valueChange = function(obj,event){
if(obj.scrollHeight<self.opts.maxheight){
//实时监听输入框值变化 HTML5事件
addEvent(self.textarea,'input propertychange', function(event) {
if(this.scrollHeight<self.opts.maxheight){
css(self.textarea,{
'height':obj.scrollHeight + "px",
'height':this.scrollHeight + "px",
'overflow':"hidden"
});
}else{
Expand All @@ -183,16 +187,8 @@
});
}
css(self.preview,{'height':self.textarea.offsetHeight + "px"});
self.opts.value = obj.value
self.opts.value = this.value
self.callback(event,self.opts)
}

//实时监听输入框值变化 HTML5事件
addEvent(self.textarea,'input', function(event) {
valueChange(this,event)
});
addEvent(self.textarea,'propertychange', function(event) {
valueChange(this,event)
});
return this;
}
Expand Down

0 comments on commit 2d5cfd6

Please sign in to comment.