淡入 jquery 加载调用

发布于 2024-10-02 21:40:34 字数 287 浏览 0 评论 0原文

我有一个关于 jquery 的非常基本的问题。但是我不知道该怎么做,所以这就是为什么我来这里寻求你的帮助。这是我的代码:

编辑: Link

如您所见,我想将 page.html 加载到名为 #target 的 div 中。我还想做但不起作用的是让 page.html 在加载时淡入。正确的方法是什么?

此致

I have a really basic question about jquery. However I don't know how to this so that's why i'm here looking for your help. This is my code:

Edit: <a href="javascript:$('#target').load('page.html').fadeIn('1000');">Link</a>

As you see i want to load page.html into a div called #target. What I also want to do wich doesen't work is to make page.html fade in when it loads. What's the right way to that?

Best Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2024-10-09 21:40:34

首先,您应该将 Javascript 代码放在元素本身之外。这做起来相当简单。它使您的 HTML 和 Javascript 更容易理解,并最终允许更有组织的代码。首先,为您的 a 元素指定一个 id

<a href='#' id='loadPage'>Link</a>

然后在 script 标记中进行调用:

<script type="text/javascript">
    $(document).ready(function() { // when the whole DOM has loaded
        $('#loadPage').click(function(){ // bind a handler to clicks on #loadPage
            $('#target')
                .hide() // make sure #target starts hidden
                .load('page.html', function() {
                    $(this).fadeIn(1000); // when page.html has loaded, fade #target in
                });
        });
    });
</script>

编辑 要发表评论,是的,您可以在 a 标记中使用 URL,然后使用 this.href

<a href='page.html' id='loadPage'>Link</a>
<script type="text/javascript">
    $(document).ready(function() {
        $('#loadPage').click(function(e){
            e.preventDefault();
            $('#target')
                .hide()
                .load(this.href, function() {
                    $(this).fadeIn(1000);
                });
        });
    });
</script>

First, you should put your Javascript code outside the element itself. This is fairly simple to do. It makes your HTML and Javascript much more easily comprehensible and ultimately allows much more organised code. First, give your a element an id:

<a href='#' id='loadPage'>Link</a>

Then make your call in a script tag:

<script type="text/javascript">
    $(document).ready(function() { // when the whole DOM has loaded
        $('#loadPage').click(function(){ // bind a handler to clicks on #loadPage
            $('#target')
                .hide() // make sure #target starts hidden
                .load('page.html', function() {
                    $(this).fadeIn(1000); // when page.html has loaded, fade #target in
                });
        });
    });
</script>

Edit To comment, yes you can use a URL in the a tag and then use this.href.

<a href='page.html' id='loadPage'>Link</a>
<script type="text/javascript">
    $(document).ready(function() {
        $('#loadPage').click(function(e){
            e.preventDefault();
            $('#target')
                .hide()
                .load(this.href, function() {
                    $(this).fadeIn(1000);
                });
        });
    });
</script>
萌化 2024-10-09 21:40:34

尝试

$('#target').load('page.html',{},function(){$('#target').fadeIn(1000)});

加载有一个完整的处理程序(参见文档

Try

$('#target').load('page.html',{},function(){$('#target').fadeIn(1000)});

load has a complete handler (see doc)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文