【问题标题】:`keyup` function not triggering with `iframe` element`keyup` 函数未使用 `iframe` 元素触发
【发布时间】:2015-03-08 11:53:34
【问题描述】:

我收到了一个iframe,我在其中找到了一个元素。我尝试使用editable div 添加keyup 事件,但它不起作用...

代码如下:

var iframebody = $(editorid).siblings().find('iframe').contents().find('body');

    var div = $('<div />', 
              {text:textstring, 
                class:'txtMsg mceNonEditable',
                "data-mce-contenteditable":"false",
                css:{'background':'#f2f2f2','margin-bottom':'5px'}});


    var msgDiv = $('<div />', 
                {html:msgText,class:'msgText mceEditable', 
                "data-mce-contenteditable":"true", 
                css:{'margin-bottom':'5px','outline':'0'}})
                .keyup(function(){
                    console.log('keyup'); //not working
                });


    var appender = iframebody.append(div).append(msgDiv);

    console.log(iframebody.find('.mceNonEditable').get(0)); //i am getting the object.

    iframebody.find('.mceNonEditable').on('keyup', function () {
        console.log('hi'); //not working!
    });

谁能解释为什么这种方法不起作用?

【问题讨论】:

标签: jquery html iframe


【解决方案1】:

根据Jquery官网find description是

获取当前匹配元素集中每个元素的后代,由选择器、jQuery 对象或元素过滤。

而on methods description 是

将一个或多个事件的事件处理函数附加到所选元素。

因为on 是按下或释放等事件,但find 不是事件所以

console.log('hi'); 

工作中

我想如果你尝试这样的事情

 iframebody.find('.mceNonEditable').trigger('keyup',function () {
        console.log('hi'); //not working!
    });

那么你可能会得到你想要的结果

【讨论】:

    猜你喜欢
    • 2020-12-14
    • 1970-01-01
    • 2016-01-14
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2023-03-19
    • 2012-02-08
    相关资源
    最近更新 更多