Opera中的Document.body [英] Document.body in Opera

查看:78
本文介绍了Opera中的Document.body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在检索到body标签的链接时遇到了问题.我尝试过:

I've got a problem with retrieving link to the body tag. I've tried:

  1. document.body,很遗憾,它为空
  2. 在枚举文档的子元素时按tagName搜索正文,但仅找到head标签:(
  3. document.getElementsByTagName,返回未定义
  1. document.body, unfortunately it is null
  2. search body by tagName while enumerating childs of document, it's found only the head tag :(
  3. document.getElementsByTagName, return undefined

我正在尝试在onload事件处理程序中获取指向body标签的链接.这是页面的HTML代码:

I'm trying to get link to the body tag in onload event handler. This is a HTML code of the page:

<html>
    <head>
        <title>Some page</title>
        <script src="/adv.js" type="text/javascript"></script>
    </head>
    <body>
        This is text
    </body>
</html>

这是adv.js的源代码:

(function () {

    var myRandom = function (min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    };

    var myFunction = function () {
        var newScriptAddr = '/adLoader.js?r=' + myRandom(1,1000000);

        var fileref = document.createElement('script');
        if (typeof fileref != "undefined")
        {
           fileref.setAttribute("type", "text/javascript");
           fileref.setAttribute("src", newScriptAddr);

           document.getElementsByTagName("head")[0].appendChild(fileref);
        }
    };

    if (window.onload)
    {
        var currOnLoad = window.onload;
        window.onload = function () {
            currOnLoad();
            myFunction();
        };
    }
    else
        window.onload = myFunction();

}) ();

adLoader.js的源代码:

(function () {

    var mainCnt = document.createElement('div');
    mainCnt.appendChild(document.createTextNode('The text'));

    var _body = document.body;

    if (!_body)
    {
        var htmlTag = document.documentElement;
        for(var i = 0; i < htmlTag.childNodes.length; i++)
        {
            if (htmlTag.childNodes[i].nodeName.toLowerCase() == 'body')
            {
                _body = htmlTag.childNodes[i];
                break;
            }
        }
    }

    if (!_body)
        _body = document.getElementsByTagName('BODY') [0];

    if (!_body)
        _body = document.getElementsByTagName('body') [0];

    if (_body)
        _body.appendChild(mainCnt);
    else
        alert('WTF!!');

}) ();

浏览器是Opera 11.10 OS Ubuntu Linux.有没有办法获得此链接?我的目的是将带有position:fixed的div添加到body标签.

Browser is Opera 11.10 OS Ubuntu Linux. Is there way to get this link? My aim is to add div with position:fixed to the body tag.

推荐答案

myFunction()不返回函数,因此您正在将undefined分配给window.onload.您可能是说window.onload = myFunction;

myFunction() doesn't return a function, so you are assigning undefined to window.onload. You probably mean window.onload = myFunction;

无论如何,如目前所写,代码会立即运行,并且没有到达body元素.

Anyway, as written at the moment, the code runs immediately and the body element hasn't been reached.

这篇关于Opera中的Document.body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆