[英]How to replace some text in a text area when a link is clicked
I'm trying to find and replace a users website address with their shortened url in a text area when a link is clicked. 单击链接时,我正在尝试在文本区域中用其缩短的URL查找和替换用户的网站地址。 I haven't got very far but at the moment I can get it to replace all the text in the text area
我还没走很远,但是目前我可以替换掉文本区域中的所有文本
<a href="javascript:;" class="short_url_link"><%= @user.short_url %></a>
$(function(){
$(".short_url_link").click(function(){
$( "#mention" ).val($(this).text());
});
});
So when the user clicks on the short url link the website address ( @user.website
) in the text area is found and replaced with the short url ( @user.short_url
) 因此,当用户单击短URL链接时,将在文本区域中找到网站地址(
@user.website
)并将其替换为短URL( @user.short_url
)
$(function(){
$(".short_url_link").click(function(e){
e.preventDefault();
$( "#mention" ).val($(this).text());
});
});
reference event.preventDefault 参考event.preventDefault
What about doing the things like that: 那么做这样的事情呢:
$(function(){
$(".short_url_link").click(function(){
var mention = $("#mention");
var prevousText = mention.text();
var shortURL = $(this).text();
var regex = new RegExp(("<%= @user.website %>").replace(/\//g, "\\/").replace(/\./g, '\\.'), "i");
mention.val(previousText.replace(regex, shortURL));
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.