在生成的元素上绑定事件并获取其当前状态
这是一个在元素上绑定 click
事件的插件,它生成一个带有
如何获取 onKeyPress 函数来获取
$.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)
}()
})
})
}
Here is a plug-in that binds a click
event on an element, which generates a container with a <textarea>
and then i bind a keypress
event on this new <textarea>
, all is ok, but inside the keypress
handler i can only get the state of text().length
from when the initial click
happened.
How do i get the onKeyPress function to get the state of the <textarea>
at the time the keypress
happened?
$.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)
}()
})
})
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试:
抱歉,尚未测试
You could try:
Haven't tested as yet sorry
我遇到了问题,
而不是通过
.html()
方法获取当前值,我使用了
.val()
原因如下:
在
I got the problem,
Instead of getting the current value via the
.html()
method,I used
.val()
Here is why:
The
.html()
method when used on a<textarea>
looks only at the initial state of thetextarea
, When you have.val()
it samples the most current value.