Jquery:如何影响父级而不影响子级?

发布于 2024-08-25 18:13:10 字数 327 浏览 5 评论 0原文

有没有办法在更改父级时不影响父级内部的子级?

<p>old text <a class="mylink">old link text</a></p>

$("a.mylink").click(function() {
        $(this).parent().text("new text for p");
        $(this).text("new link text for a");
    });
});

上面似乎完全摆脱了链接文本。我基本上希望能够在单击发生时更改两个文本。

谢谢。

Is there a way to not affect children that are inside a parent when the parent is being changed?

<p>old text <a class="mylink">old link text</a></p>

$("a.mylink").click(function() {
        $(this).parent().text("new text for p");
        $(this).text("new link text for a");
    });
});

The above seems to get rid of the link text completely. I'd basically like to be able to change both texts when the click happens.

Thank you.

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

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

发布评论

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

评论(2

凉世弥音 2024-09-01 18:13:10

不是 JQuery 解决方案,但这有效:

$("a.mylink").click(function() {
    $(this)[0].previousSibling.nodeValue = "new text for p";
    $(this).text("new link text for a");
});

Not a JQuery solution, but this works:

$("a.mylink").click(function() {
    $(this)[0].previousSibling.nodeValue = "new text for p";
    $(this).text("new link text for a");
});
把昨日还给我 2024-09-01 18:13:10

你可以试试这个技巧。有用

 $("a.mylink").click(function() {
       $(this).parent().html("new text for p" + $(this).text("new link text for a").html());
   });

You can try this trick. It works

 $("a.mylink").click(function() {
       $(this).parent().html("new text for p" + $(this).text("new link text for a").html());
   });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文