Loading an external .htm file to a div with javascript [duplicate]

自古美人都是妖i 提交于 2019-12-13 08:03:30

问题


Possible Duplicate:
Loading an external .htm with javascript into a div
Loading an external .htm file into a div with javascript

This is my entire code:

<html>

<head>

</head>

<body>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">
$(function(){
    $('.ajax') .click(function(e){
        e.preventDefault()
        $('#content').load( 'file.htm' )
    })
})
</script>

<div id="content">
    <p>random text</p>
</div>

<div><a href="#" class="ajax">Link</a></div>


</body>

</html>

The code works fine for loading an external file into a div in firefox, but nothing happens in chrome and IE, when I click the link. any advice ?


回答1:


While it's not the cause of the problem, it's good practice to use semicolons:

$(function(){
    $('.ajax') .click(function(e){
        e.preventDefault();
        $('#content').load( 'file.htm' );
    });
});

You'll occasionally hit some strange errors if you don't.



来源:https://stackoverflow.com/questions/11281204/loading-an-external-htm-file-to-a-div-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!