用jQuery替换锚文本

问题描述:

我要替换html锚文本:

i want to replace the text of a html anchor:

<a href="index.html" id="link1">Click to go home</a>

现在我要替换文本点击回家"

now i want to replace the text 'click to go home'

我已经尝试过了:

alert($("link1").children(":first").val());
alert($("link1").children(":first").text());
alert($("link1").children(":first").html());

但这都给了我null或空字符串

but it all gives me null or an empty string

尝试

$("#link1").text()

访问元素内的文本. #表示您正在按ID搜索.您不需要的是child元素,因此不需要children().相反,您想访问jQuery函数返回的元素内的文本.

to access the text inside your element. The # indicates you're searching by id. You aren't looking for a child element, so you don't need children(). Instead you want to access the text inside the element your jQuery function returns.