代码之家  ›  专栏  ›  技术社区  ›  Tom

jquery:如何影响父级而不是子级?

  •  0
  • Tom  · 技术社区  · 14 年前

    当父对象被更改时,是否有方法不影响父对象内部的子对象?

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

    上面的链接文字似乎完全去掉了。我基本上希望在点击时能同时更改这两个文本。

    谢谢您。

    2 回复  |  直到 12 年前
        1
  •  2
  •   poke    14 年前

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

    $("a.mylink").click(function() {
        $(this)[0].previousSibling.nodeValue = "new text for p";
        $(this).text("new link text for a");
    });
    
        2
  •  1
  •   Hai Lu    12 年前

    你可以试试这个把戏。它工作

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