Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从textarea获取位置为的选定字符串,尽管它存在多次_Javascript_Html_Substring_Selection_Textselection - Fatal编程技术网

Javascript 从textarea获取位置为的选定字符串,尽管它存在多次

Javascript 从textarea获取位置为的选定字符串,尽管它存在多次,javascript,html,substring,selection,textselection,Javascript,Html,Substring,Selection,Textselection,函数addURL(e){ var n=document.selection?document.selection.createRange()。文本:e.value.substring(e.selectionStart,e.selectionEnd); 变量a=n+'[link]'; e、 值=e.值.替换(n,a); } 这是一个测试。这是一个测试。这是一个测试。这是因为您使用的replace将只替换所有匹配值的第一个实例。您需要使用子字符串 函数addURL(e){ var n=wind

函数addURL(e){
var n=document.selection?document.selection.createRange()。文本:e.value.substring(e.selectionStart,e.selectionEnd);
变量a=n+'[link]';
e、 值=e.值.替换(n,a);
}




这是一个测试。这是一个测试。这是一个测试。
这是因为您使用的
replace
将只替换所有匹配值的第一个实例。您需要使用
子字符串

函数addURL(e){
var n=window.selection?window.selection.createRange()。文本:e.value.substring(e.selectionStart,e.selectionEnd);
变量a=n+'[link]';
e、 value=e.value.substring(0,e.selectionStart)+a+e.value.substring(e.selectionEnd,e.value.length);
}
#文本{
宽度:150px;
高度:50px;
}




这是一个测试。这是一个测试。这是一个测试。
这是因为您使用的
replace
将只替换所有匹配值的第一个实例。您需要使用
子字符串

函数addURL(e){
var n=window.selection?window.selection.createRange()。文本:e.value.substring(e.selectionStart,e.selectionEnd);
变量a=n+'[link]';
e、 value=e.value.substring(0,e.selectionStart)+a+e.value.substring(e.selectionEnd,e.value.length);
}
#文本{
宽度:150px;
高度:50px;
}




这是一个测试。这是一个测试。这是一个测试。
您希望链接到哪里?目前,它似乎将进入文本区域的开始。您想让它紧跟在用户突出显示的单词或短语之前吗?@AHaworth我想
[link]
紧跟在突出显示的字符串之后。这不适合你吗?你确定这是文本区的开始吗?啊,我只是指向而不是选择,是的,没关系。你想链接到哪里?目前,它似乎将进入文本区域的开始。您想让它紧跟在用户突出显示的单词或短语之前吗?@AHaworth我想
[link]
紧跟在突出显示的字符串之后。这不适合你吗?你确定这是文本区的开始吗?啊,我是指着而不是选择,是的,没关系。