使用javascript将外部.htm文件加载到div [英] Loading an external .htm file to a div with javascript

查看:105
本文介绍了使用javascript将外部.htm文件加载到div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

使用javascript将外部.htm加载到div中

使用javascript将外部.htm文件加载到div中

这是我的完整代码:

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>

这段代码在firefox中加载外部文件时效果不错,但在Chrome中没有任何反应, IE,当我点击链接。任何建议?

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 ?

推荐答案

虽然这不是问题的原因,但最好使用分号:

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.

这篇关于使用javascript将外部.htm文件加载到div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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