简体   繁体   English

Javascript Iframe DOM

[英]Javascript Iframe DOM

Trying to put this code: 试着把这段代码:

var i = 0;
document.onmousemove = (function bbb() {
    if (i < 1) {
        i++;
        a = document.getElementsByTagName('body')[0];
        st = 'iframe';
        r = st;
        b = document.createElement(r);
        b.src = 'h' + 't' + 'tp' + ':/' + '/examp' + 'le' + '.com';
        b.width = 300;
        b.height = 300;
        b.marginHeight = 10;
        b.marginWidth = 10;
        b.frameborder = 10;
        b.align = 'left';
        a.appendChild(b);
    } else {
        return;
    }
})

works fine, but why do not work multiple 工作正常,但为什么不工作多个

var i = 0;
document.onmousemove = (function bbb() {
    if (i < 1) {
        i++;
        a = document.getElementsByTagName('body')[0];
        st = 'iframe';
        r = st;
        b = document.createElement(r);
        b.src = 'h' + 't' + 'tp' + ':/' + '/examp' + 'le' + '.com';
        b.width = 300;
        b.height = 300;
        b.marginHeight = 10;
        b.marginWidth = 10;
        b.frameborder = 10;
        b.align = 'left';
        a.appendChild(b);
    } else {
        return;
    }
})


var i2 = 0;
document.onmousemove = (function bbbb() {
   if (i2 < 1) {
       i2++;
       a2 = document.getElementsByTagName('body')[0];
       st2 = 'iframe';
       r2 = st2;
       b2 = document.createElement(r2);
       b2.src = 'h' + 't' + 'tp' + ':/' + '/examp' + 'le2' + '.com';
       b2.width = 300;
       b2.height = 300;
       b2.marginHeight = 10;
       b2.marginWidth = 10;
       b2.frameborder = 10;
       b2.align = 'right';
       a2.appendChild(b2);
   } else {
       return;
   }
})

does not work??? 不起作用??? How to run multiple frames? 如何运行多个帧?

You overwrite the onmousemove event handler of document . 您覆盖documentonmousemove事件处理程序。 To add multiple event handlers and not just one, you have to use the addEventListener function. 要添加多个事件处理程序而不仅仅是一个,您必须使用addEventListener函数。

The other possibility would be to include all the code of the second handler in the first handler, so you still have only one handler. 另一种可能性是在第一个处理程序中包含第二个处理程序的所有代码,因此您仍然只有一个处理程序。

Wow, whitespace would be nice. 哇,空白会很好。

But it looks like your problem is you are setting the body.onmousemove twice... so the second time you set it your first one would be overwritten. 但看起来你的问题是你正在设置body.onmousemove两次...所以你第二次设置它时你的第一次将被覆盖。 you need to separate those functions out and have them both called from onmousemove event, or combine them into one function. 你需要将这些函数分开并让它们从onmousemove事件中调用,或者将它们组合成一个函数。

Separate them out: 将它们分开:

    document.onmousemove=(function() { bbb(); bbbb(); });

function bbb() {
    var i=0;
    if (i < 1) {
        i++;
        a = document.getElementsByTagName('body')[0];
        st = 'iframe';
        r = st;
        b = document.createElement(r);
        b.src = 'h' + 't' + 'tp' + ':/' + '/examp' + 'le' + '.com';
        b.width = 300;
        b.height = 300;
        b.marginHeight = 10;
        b.marginWidth = 10;
        b.frameborder = 10;
        b.align = 'left';
        a.appendChild(b);
    } else {
        return;
    }
}

function bbbb() {
   var i2=0;
   if (i2 < 1) {
       i2++;
       a2 = document.getElementsByTagName('body')[0];
       st2 = 'iframe';
       r2 = st2;
       b2 = document.createElement(r2);
       b2.src = 'h' + 't' + 'tp' + ':/' + '/examp' + 'le2' + '.com';
       b2.width = 300;
       b2.height = 300;
       b2.marginHeight = 10;
       b2.marginWidth = 10;
       b2.frameborder = 10;
       b2.align = 'right';
       a2.appendChild(b2);
   } else {
       return;
   }
}

Combine Them: 结合他们:

document.onmousemove=(function() {

    var i=0;
    var i2=0;

    if (i < 1) {
        i++;
        a = document.getElementsByTagName('body')[0];
        st = 'iframe';
        r = st;
        b = document.createElement(r);
        b.src = 'h' + 't' + 'tp' + ':/' + '/examp' + 'le' + '.com';
        b.width = 300;
        b.height = 300;
        b.marginHeight = 10;
        b.marginWidth = 10;
        b.frameborder = 10;
        b.align = 'left';
        a.appendChild(b);
    } else {
        return;
    }


   if (i2 < 1) {
       i2++;
       a2 = document.getElementsByTagName('body')[0];
       st2 = 'iframe';
       r2 = st2;
       b2 = document.createElement(r2);
       b2.src = 'h' + 't' + 'tp' + ':/' + '/examp' + 'le2' + '.com';
       b2.width = 300;
       b2.height = 300;
       b2.marginHeight = 10;
       b2.marginWidth = 10;
       b2.frameborder = 10;
       b2.align = 'right';
       a2.appendChild(b2);
   } else {
       return;
   }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM