开发者

Jquery: How to affect parent WHILE not children?

开发者 https://www.devze.com 2022-12-23 00:37 出处:网络
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>

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 chan开发者_如何学Pythonge both texts when the click happens.

Thank you.


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


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());
   });
0

精彩评论

暂无评论...
验证码 换一张
取 消