开发者

Load code into HTML using JQuery?

开发者 https://www.devze.com 2023-02-09 07:38 出处:网络
I know it\'s possible, and I\'ve looked at nume开发者_C百科rous examples. And I\'m afraid I already know the answer to my question, but perhaps I\'m missing something.

I know it's possible, and I've looked at nume开发者_C百科rous examples. And I'm afraid I already know the answer to my question, but perhaps I'm missing something. I was thinking that this does not work because I am running this code locally, and not on a server.

In my head tag:

<script type="text/javascript" src="jquery-1.5.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
        $("#test").load("test.html");
        } );
</script>

In my body tag:

<div id="test"></div>

My external code (inside 'test.html")

<div>This is what you should be seeing!</div>

Shouldn't this work? Additionally, in my external html document, do I need set it up with all the extra tags, so that the document would look like:

<html>
    <head>
        <title>
        </title>
    </head>
    <body>

<div>This is what you should be seeing!</div>
</body>
</html>

All my files are in the same directory.

Thanks.


Well your code should work on webserver! Please let me know if you are doing it from you desktop.

<script src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
    $("#test").load("./test.html"); //  also try /test.html
    } );
</script>
<div id="test"></div>


Simple

$.get('test.html', function(data) {
    $('#test').html(data);
});

The .load() have a different function, to bind event handlers on load event

UPDATE:

To add the data insted replace try the append

$('body').append(data)


Use a optional selector after the URL to only put the <body>'s content (or something else) into the target:

   // will load the contents of test.html's body-element into #test  
   $("#test").load("test.html body");
0

精彩评论

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