2010-03-13 77 views
0

当父母被更改时,是否有办法不影响父母内部的孩子?jquery:如何影响父母而不是孩子?

<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

不是一个jQuery的解决方案,但这个工程:

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

确实,谢谢。 – Tom 2010-03-13 18:50:24

1

你可以试试这一招。它的工作原理

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