在生成的元素上绑定事件并获取其当前状态

发布于 2024-09-25 02:53:19 字数 1404 浏览 2 评论 0原文

这是一个在元素上绑定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-10-02 02:53:19

您可以尝试:

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)
                }()

            })
        })

抱歉,尚未测试

You could try:

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)
                }()

            })
        })

Haven't tested as yet sorry

埋情葬爱 2024-10-02 02:53:19

我遇到了问题,

而不是通过 .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 the textarea, When you have .val() it samples the most current value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文