jQuery更改子文本 [英] jQuery change child text
问题描述
我有一个jquery函数,可以使用jquery的文本"函数更改元素内的文本.这样的td元素内部是子"a"标签.
I have a jquery function that changes a text inside a element using a jquery "text" function. Inside this td element is child "a" tag like this.
<td class="v3"><a href="somelink">text to change</a>
要更改的文本由td类标识.当我更改文本时,我会丢失链接.问题是如何在不触摸子级(a href)的情况下使用父级元素更改文本?
The text to change is identified by the td class. When I change the text I lose the link. Question is how do I change the text using the parent element without touching the child (a href)?
感谢
推荐答案
如果有:
<td class="v3">
<a href="somelink">text to change</a>
</td>
然后在您的函数中,您应该具有以下内容:
Then in your function you should have something like this:
$("td.v3").children("a").text("new text");
但是,这将选择属于.v3类的tds的直接子级的所有链接.在孩子之后添加.first()应该会有所帮助:
However, this will select all links that are direct children of tds with class .v3. Adding a .first() after children should help:
$("td.v3").children("a").first().text("new text");
这篇关于jQuery更改子文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!