使用Mootools在iframe内容中选择元素?

时间:2014-02-14 16:15:55

标签: javascript mootools

有没有办法可以使用Mootools选择器来选择iframe内容中的元素?

$('#myIframe input').addEvent('focus', function() {
    // Do something
});

提前谢谢!

2 个答案:

答案 0 :(得分:3)

如果iFrame位于同一个域中并且您在其中使用Mootools,则可以尝试:

$('myIframe').contentDocument.getElements('input').addEvent(

Example

否则试试这个:

$('myIframe').contentDocument.querySelector('input').addEventListener(

Example


编辑:

如果您想捕获load事件,然后添加可以使用的focus侦听器:

var iframe = new IFrame({
    src: '/RN95f/3/show',
    styles: {
        border: '2px solid #ccf'
    },
    events: {
        load: function () {
            alert('The iframe has finished loading.');
            this.contentDocument.getElements('input').addEvent('focus', function () {
                // Do something
                alert('focus on input detected!');
                console.log(this);
            });
        }
    }
});
$(document.body).adopt(iframe);

Example

答案 1 :(得分:1)

塞尔吉奥的回答是正确的,谢谢塞尔吉奥的回复!但是为了让它工作,我需要确保在找到输入元素之前加载iframe内容......

$('myIframe').onload = function() {
    $('myIframe').contentDocument.getElements('input').addEvent('focus', function() {
        // Do something
    });
};