jQuery 使用each() 循环遍历类 - 问题

发布于 2024-12-06 02:59:49 字数 646 浏览 0 评论 0原文

我使用此代码循环遍历每个“.accessoryrow”,然后选择“dialog”+ counter 和“.see-details”+ counter。因此,第一次循环时,它选择dialog1类和see-details1类;第二次dialog2、see-details2等等。我认为我没有正确地向选择器添加计数器。请纠正我。谢谢

代码:

  var counter = 1;
        $(function () {
        $(".accessoryrow").each(function() {
            $(".dialog" + counter).dialog({
                autoOpen: false,
                show: "blind",
                hide: "fade"
            });

            $(".see-details" + counter).click(function () {
                $(".dialog" + counter).dialog("open");
                return false;
            });
            counter++;
        });

I am using this code to loop through each ".accessoryrow" and then select "dialog" + counter and ".see-details" + counter. So first time when loop goes by it selects dialog1 class and see-details1 class; second time dialog2, see-details2 and so on. I think I am not correctly adding counter to the selector. Please correct me. Thank you

CODE:

  var counter = 1;
        $(function () {
        $(".accessoryrow").each(function() {
            $(".dialog" + counter).dialog({
                autoOpen: false,
                show: "blind",
                hide: "fade"
            });

            $(".see-details" + counter).click(function () {
                $(".dialog" + counter).dialog("open");
                return false;
            });
            counter++;
        });

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

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

发布评论

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

评论(2

梦里南柯 2024-12-13 02:59:49

问题是,在单击链接之前,不会评估 $(".dialog" + counter).dialog("open"); 行。因此,它使用的是 counter 的值,该值是当时的当前值。更好的方法是完全删除计数器,并使用 jQuery 选择器来选择正确的 .dialog。

如果没有 HTML,我无法说出它应该是什么样子,但您会希望单击功能看起来像

 $(".see-details").click(function () {
        $(this).sibling(".dialog").dialog("open");
        return false;
    });

当然,假设 .dialog 元素实际上是 .see 的同级元素-细节。如果不是,您需要多遍历一下树。

The problem is that the $(".dialog" + counter).dialog("open"); line isn't getting evaluated until the link is clicked. Thus it's using the value of counter which is current as of then. A better way to do it would be to take out the counter altogether, and use jQuery selectors to select the correct .dialog.

Without the HTML, I can't say what it should look like, but you're going to want the click function to look like something along the lines of

 $(".see-details").click(function () {
        $(this).sibling(".dialog").dialog("open");
        return false;
    });

Of course, that assumes that the .dialog element is actually a sibling of .see-details. You'll need to traverse the tree a bit more if it isn't.

掀纱窥君容 2024-12-13 02:59:49

试试这个(未经测试):

$(function () {
    $(".accessoryrow").each(function(index) {
        $(".dialog" + (index + 1)).dialog({
            autoOpen: false,
            show: "blind",
            hide: "fade"
        });

        $(".see-details" + (index + 1)).click(function () {
            $(".dialog" + (index + 1)).dialog("open");
            return false;
        });
    });

Index 将循环编号传递给函数。它从 0 开始,我认为你需要它从 1 开始,所以我在每个使用它的地方添加了 1。

Try this (untested):

$(function () {
    $(".accessoryrow").each(function(index) {
        $(".dialog" + (index + 1)).dialog({
            autoOpen: false,
            show: "blind",
            hide: "fade"
        });

        $(".see-details" + (index + 1)).click(function () {
            $(".dialog" + (index + 1)).dialog("open");
            return false;
        });
    });

Index passes the loop number to the function. It starts from 0, where I think you need it to start at 1, so I've added 1 to each where it's used.

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