jQuery .load() 函数的 JavaScript 语法

发布于 2024-10-18 20:54:46 字数 929 浏览 1 评论 0 原文

我刚刚尝试制作一个简单的脚本来使用 ajax 加载页面的新部分。用于更改相关文本颜色的类删除/添加效果很好。然而,新的html似乎没有出现。我有一种感觉,这与我的一般 js 语法有关,但我无法解决。

Javascript:

    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#page_menu a").click(function() {
            $("#page_menu p").removeClass("current");
            $(this).children().addClass("current");

            var project = $(this).attr("name");
            var loadUrl = project + ".html";

            $("#project_image").load(loadUrl);

            return false;
            });
        });
    </script>

html 中锚标记的一个示例是:

<a name=example href="#">Example</a>

我要加载的 html 文件将被称为“example.html”,其中的代码:

<h1>Hello</h1>

我确信它非常简单,但我只是没看到而已!

干杯,

丰富

I've just attempted to make a simple script to use ajax to load a new part of a page. The class remove/add to change the relevant text colour works fine. However, the new html does not seem to appear. I have a feeling this is to do with my general js syntax but I can't work it out.

Javascript:

    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#page_menu a").click(function() {
            $("#page_menu p").removeClass("current");
            $(this).children().addClass("current");

            var project = $(this).attr("name");
            var loadUrl = project + ".html";

            $("#project_image").load(loadUrl);

            return false;
            });
        });
    </script>

An example of an anchor tag in the html would be:

<a name=example href="#">Example</a>

The html file I'm looking to load would be called "example.html" and the code in it:

<h1>Hello</h1>

I'm sure it's pretty straight-forward but I'm just not seeing it!

Cheers,

Rich

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

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

发布评论

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

评论(2

养猫人 2024-10-25 20:54:46

我会直接使用锚点的 href

<a href="example.html">Example</a>
<div id="project_image"></div>

然后 AJAXify 它:

$(function() {
    $('#page_menu a').click(function() {
        $('#page_menu p').removeClass('current');
        $(this).children().addClass('current');
        $('#project_image').load(this.href);
        return false;
    });
});

I would use the href of the anchor directly:

<a href="example.html">Example</a>
<div id="project_image"></div>

And then AJAXify it:

$(function() {
    $('#page_menu a').click(function() {
        $('#page_menu p').removeClass('current');
        $(this).children().addClass('current');
        $('#project_image').load(this.href);
        return false;
    });
});
弱骨蛰伏 2024-10-25 20:54:46

锚点肯定有一个 name 属性,所以这部分没问题..但为了让事情更清晰,请将锚点更改为:

<a href="example.html">Example</a>

为了长度起见,您可以使用 $(document).ready 的简写语法,并且还在一条链中进行类更改。然后只需加载 href 中指定的页面并查看请求是否实际有效,添加回调,如下所示:

$(function() {
    $("#page_menu a").click(function(e) {
        $("#page_menu p").removeClass("current").filter(this).addClass("current");

        $("#project_image").load(this.href, function(res) {
            // This will allow you to see the response from the server without having to dig through requests
            // If you don't have a console for some reason, just change this to alert()
            console.log(res);
        });

        e.preventDefault();
    });
});

Anchor's most certainly do have a name attribute, so that part would be okay.. but to make things cleaner, change your anchor to:

<a href="example.html">Example</a>

For length sake you can use shorthand syntax for $(document).ready, and also do the class changes in one chain. Then just load the page specified in the href and to see if the request actually worked, add a callback, like so:

$(function() {
    $("#page_menu a").click(function(e) {
        $("#page_menu p").removeClass("current").filter(this).addClass("current");

        $("#project_image").load(this.href, function(res) {
            // This will allow you to see the response from the server without having to dig through requests
            // If you don't have a console for some reason, just change this to alert()
            console.log(res);
        });

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