ReplaceWith 隐藏元素然后显示在原始位置,而不是目标位置

发布于 2024-10-07 04:00:59 字数 405 浏览 0 评论 0原文

链接:http://jsfiddle.net/7GGeX/24/

点击1,2中的链接,3 命令你就会明白我为什么感到困惑了。

在replaceWith中使用函数是否会否定替换的定位?

$(document).ready(function () {
    $(".click1").click(function () {
        $("#one").replaceWith(function () {
            $('#replace1').show();
        });
        return false;
    });

感谢您的帮助!

Link: http://jsfiddle.net/7GGeX/24/

Click on the links in 1,2,3 order and you'll see why I'm confused.

Does using a function inside replaceWith negate the positioning of the replacement?

$(document).ready(function () {
    $(".click1").click(function () {
        $("#one").replaceWith(function () {
            $('#replace1').show();
        });
        return false;
    });

Thanks for the help!

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

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

发布评论

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

评论(1

随波逐流 2024-10-14 04:00:59

您需要返回要用作替换的值。

$("#two").replaceWith(function() {
      // return the element
    return $('#replace2').show();
});

或者不传递函数:

$("#two").replaceWith($('#replace2').show());

由于您没有显式返回任何内容,因此会显示 replace div,然后返回 undefined,从而有效地用任何内容替换原始内容。

You need to return the value you want to use as the replacement.

$("#two").replaceWith(function() {
      // return the element
    return $('#replace2').show();
});

or don't pass a function:

$("#two").replaceWith($('#replace2').show());

Since you weren't returning anything explicitly, the replace div was being shown, then undefined was returned, effectively replacing the original with nothing.

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