开发者

Insert DIV Just after <body> tag

开发者 https://www.devze.com 2023-03-11 10:12 出处:网络
I have the following code: <script type=\"text/javascript\"> $(document).ready(function() { $(\'<div id=\"tools\" style=\"text-align:right;float:right;\"><input type=\"button\" value=\

I have the following code:

<script type="text/javascript">
    $(document).ready(function() {
        $('<div id="tools" style="text-align:right;float:right;"><input type="button" value="Print this page" onclick="window.print();return false;开发者_开发技巧" /><input type="button" value="Save this page" onclick="go_saveas();return false;" /></div>').insertBefore('body');
    });
</script>

Basically, I need to insert that whole Div just right after the <body> tag:

</head>
<body>
    <div id="tools"..
...

Which works in Firefox but doesn't work in IE 7, what do I have to change to fix this?


You're using insertBefore. That will try to put it between head and body; not what you want. Try prependTo.


http://jsfiddle.net/XDFMt/:

<script type="text/javascript">
    $(document).ready(function() {

        $('<div id="tools" style="text-align:right;float:right;"><input type="button" value="Print this page" onclick="window.print();return false;" /><input type="button" value="Save this page" onclick="go_saveas();return false;" /></div>')
            .prependTo('body');

    });
</script>


Instead of using insertBefore, using prependTo. This way:

<script type="text/javascript">
    $(document).ready(function() {
        $('<div id="tools" style="text-align:right;float:right;"><input type="button" value="Print this page" onclick="window.print();return false;" /><input type="button" value="Save this page" onclick="go_saveas();return false;" /></div>').prependTo('body');
    });
</script>

The insertBefore inserts your code before the tag . That's why it gives you problems. You were lucky that Firefox corrected it to what you wanted. Now, prependTo inserts it inside your tag, but before all its content. ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消