【问题标题】:How to replace <a> text [duplicate]如何替换<a>文本[重复]
【发布时间】:2014-10-10 17:16:45
【问题描述】:

我想替换标签&lt;a&gt;内的文本(“My Text”)
这是我的代码:

<a id="myID" href="#">
    My Text
    <img src="/layout/images/blank.gif">
</a>

我试过了:

$( "#myID" ).text( "New Text" );

但我返回此错误:

TypeError:字符串不是函数
消息:“字符串不是函数”
堆栈:(...)
获取堆栈:function () { [本机代码] }
set stack: function () { [native code] }
proto: Error

和:

$( "#myID" ).html( "New Text" );

类型错误:未定义不是函数
消息:“未定义不是函数”
堆栈:(...)
获取堆栈:function () { [本机代码] }
set stack: function () { [native code] }
proto: Error

【问题讨论】:

标签: jquery text replace


【解决方案1】:

您可以将html()与回调函数一起使用

$("#myID").html(function(i, v) {
    return v.replace("My Text", "New Text");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a id="myID" href="#">
    My Text
    <img src="/layout/images/blank.gif">
</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2013-05-13
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    相关资源
    最近更新 更多