尝试输入以下代码:

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


不起作用???如何运行多个帧?

最佳答案

哇,空格会很好。

但是看来您的问题是您将body.onmousemove设置了两次...因此,第二次设置它时,您的第一个将被覆盖。您需要将这些函数分开,并从onmousemove事件中调用它们,或者将它们组合为一个函数。

将它们分开:

    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;
   }
}


合并它们:

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;
   }

});

10-04 16:05