这是一个绑定(bind)click
的插件元素上的事件,生成一个带有 <textarea>
的容器然后我绑定(bind)一个 keypress
关于此新品的事件<textarea>
,一切都好,但是在 keypress
里面处理程序我只能获取 text().length
的状态从最初click
时开始发生了。
如何通过 onKeyPress 函数获取 <textarea>
的状态当时keypress
发生了什么?
$.fn.pluginTemp = function(options){
var defaults = {
editContainer:"<div class='editContainer'><textarea></textarea></div>",
maxChar:20,
onKeyPress:function(){
console.log($(".editContainer>textarea").text())
}
}
var options = $.extend(defaults,options);
return this.each(function(i,e){
var el = $(e), initText
el.bind("click", function(){
initText = el.text();
el.parent().append(options.editContainer);
el.parent().find(".editContainer>textarea").html(initText);
// isn't this function remembering the state for when its is defined? which is on keypress?
return function(){
el.parent().find(".editContainer>textarea").bind("keypress",options.onKeyPress)
}()
})
})
}
最佳答案
你可以尝试:
return this.each(function(i,e){
var el = $(e), initText
el.live('click', function() {
initText = el.text();
el.parent().append(options.editContainer);
el.parent().find(".editContainer>textarea").html(initText);
// isn't this function remembering the state for when its is defined? which is on keypress?
return function(){
el.parent().find(".editContainer>textarea").bind("keypress",options.onKeyPress)
}()
})
})
抱歉,尚未测试
关于javascript - 在生成的元素上绑定(bind)事件并获取其当前状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3769935/