div 中的字符串替换
我想替换 #TextArea1
中的特定字符串。单击按钮时会发生这种情况。
使用下面的代码进行尝试,但无法使其工作:
$('#TextArea1').text().replace("wef","--");
What is the Correct syntax to replacement a word in a div
?
I want to replace a particular string in #TextArea1
. This happens when a button is clicked.
Trying it out with the below code, but unable to get it work:
$('#TextArea1').text().replace("wef","--");
What is the correct syntax to replace a word in a div
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将函数传递给
text()
[docs]方法,返回想要设置的值:函数参数如下:
i
是迭代中当前元素的索引txt 是当前元素的当前文本内容
函数返回的值将设置为当前元素的新值。
Pass a function to the
text()
[docs] method that returns the value you want set:The function parameters are as follows:
i
is the index of the current element in the iterationtxt
is the current text content of the current elementThe value returned from the function will be set as the new value for the current element.
你已经很接近了,试试这个:
或者一个优化的
如果它是一个
textarea
元素,你会这样做:You are close, try this:
Or an optimized one
If it's a
textarea
element, you would do:您还设置了文本:
或者,使用函数:
注意:如果这是
You have set the text also:
or, using a function:
Note: if this is a
<textarea>
, useval()
instead oftext()
.replace()
创建一个新字符串并返回它,因此它被返回到稀薄的空气中。您需要获取新字符串并将其发送回文本框。这个 jsfiddle 显示了如何操作。replace()
creates a new string and returns it, so it's getting returned into thin air. You need to grab the new string and send it back into the textbox. This jsfiddle shows how.