【问题标题】:Jquery: How to affect parent WHILE not children?Jquery:如何影响父母而不是孩子?
【发布时间】:2010-03-13 18:26:41
【问题描述】:

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

<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");
    });
});

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

谢谢。

【问题讨论】:

    标签: jquery selector parent children


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-09-25
        • 1970-01-01
        • 2022-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-11
        • 2014-12-06
        相关资源
        最近更新 更多