用jQuery替换锚文本 [英] replace anchor text with jquery
本文介绍了用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.
这篇关于用jQuery替换锚文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文