1. 程式人生 > >在textarea中滑鼠指定的位置插入字元或表情

在textarea中滑鼠指定的位置插入字元或表情

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
	</head>
	<body>
		<div class="container">
			<div class="row">
				<div class="col col-sm-12">
					<button class="btn btn-danger" data-param="{$buyer}">買家</button>
					<button class="btn btn-danger" data-param="{$address}">地址</button>
				</div>
				<div class="col col-sm-12">
					<textarea class="form-control" id="content" rows="10"></textarea>
				</div>
			</div>
		</div>
	
		<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
		<script>
		(function ($) {
			$.fn.extend({
				insertAtCaret: function (myValue) {
					var $t = $(this)[0];
					if (document.selection) {
						this.focus();
						sel = document.selection.createRange();
						sel.text = myValue;
						this.focus();
					} else if ($t.selectionStart || $t.selectionStart == '0') {
						var startPos = $t.selectionStart;
						var endPos = $t.selectionEnd;
						var scrollTop = $t.scrollTop;
						$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
						this.focus();
						$t.selectionStart = startPos + myValue.length;
						$t.selectionEnd = startPos + myValue.length;
						$t.scrollTop = scrollTop;
					} else {
						this.value += myValue;
						this.focus();
					}
				}
			});
		})(jQuery);
			
		$("button").on("click", function() {
			$("#content").insertAtCaret($(this).attr("data-param"));
		});
		</script>
	</body>
</html>

相關推薦

textarea滑鼠指定位置插入字元表情

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <link href="https://cdn.bootcss.c

點選滑鼠textarea游標當前位置插入指定字元

function insertText(obj,str) { //obj為textarea 元素,str為要插入字元 if(Switch == 1){ if (document.selection) { var sel = docum

詳解C語言在字串的指定位置插入字元

問題分析 在字串S的所有數字字元前加一個$字元,可以有兩種實現方法。方法一:用串S拷貝出另一個串T,對串T從頭至尾掃描,對非數字字元原樣寫入串S,對於數字字元先寫一個$符號再寫該數字字元,最後,在S串尾加結束標誌。使用此方法是犧牲空間,贏得時間。方法二:對串S從頭至尾掃描,當遇到數字字元時,從該字元至串

C例項----在字串指定位置插入字元

程式碼: #include <stdio.h> #include <string.h> #include <stdlib.h> void Insert (char c, int pos, char *src) {

JS 字串操作函式 往指定位置插入字元 刪除指定位置字元 替換指定位置字元

刪除指定字元 var str="xxxxxxxxabcxxxxxxxxxxxx"; var pattern = "abc"; str = str.replace(new RegExp(pattern), ""); console.log(str); 執行結果就是xxxxxxxxxxxxxxxxx

[JQuery]往textarea游標所在位置插入文字

<html> <head> <script src="jquery-1.8.1.min.js"></script> <script > $(function() { /* 在textarea處插入文字--S

python在字串的指定位置插入一個字元

# 在字串的自定位置插入字元 # 因為python中str是不可變的變數,所以藉助list來完成 str_i = "20081231" list_i = list(i) # str -> list print(list_i) list_i.insert(4, '/') #

Python:字串指定位置插入一個字元

str_1='wo shi yi zhi da da niu/n'str_list=list(str_1) nPos=str_list.index('/') str_list.insert(nPos,',') str_2="".join(str_list)print(str_2)

Aspose.Words 將word2的內容插入到word1指定位置

將word2中的內容插入到word1中的指定位置(經測試可用) 在官網找到的例子,記錄一下: public static void InsertDocumentAtBookmark(string dataDir) { Document main

python3 給字串指定位置字元

字串的話,你可以把他當作列表處理: str = 'hello world'  如果是想去掉第一個字母'o',並且知道是第5個字元, index=4 1.使用分片 new_str = str[:4]+str[5:] ; 2.迴圈讀取new_str = ''.join([str[i]

C++類單鏈表的實現(頭插、尾插、頭刪、尾刪、指定位置插入指定位置刪除、連結串列長度、清空連結串列、連結串列排序)

#include<iostream> using namespace std; class Node { public:Node():next(NULL){}Node(int n,Node *p = NULL):value(n),next(p){}int val

java 批量更改CSV檔案內容指定位置字元

有104個一樣格式的檔案只是記錄數不同 改為這個模式 把最後的2改為4 也就是2表示兩條記錄,4表示總的記錄(包括第一行和最後一行) @Test public void test4() throws Exception{

android開發 Java在指定位置插入字元

最近有個需求就是銀行卡4個空格,傳後臺是去除空格的,但是讀資料的時候要求再次加空格,搜了一下插入空格的方法發現都不適合很靠譜,於是自己寫了。 其實很簡單,廢話不多說,直接上程式碼。 //bankAccountNumber 需要插入空格的字串 //每4個插入一個空格 pri

java格式化字串,在指定位置插入指定字串,相容中英文以及特殊字元,例如:換行,用於解決生成pdf換行問題等問題

原因: 由於html轉pdf時,不能自動換行,因此才有下面的程式碼.  註釋:完全模擬html頁面的自動換行! package test; import java.io.Unsupported

使用sed在一個檔案指定位置插入另外一個檔案的內容-r命令的使用

在一個檔案的指定位置,比如說匹配到了一個單詞的行的下一行開始插入另外一個檔案的全部內容,可以使用sed的r命令。 [email protected]:~/aa$ cat data here is a test naughty loves cc and so on

自己編寫javaString替換指定位置字元的方法

static String replace(String mystring,String content, int location){//mystring 為原來字串,content為新增的字元,location為新增的位置return mystring.substring

在文件指定位置寫入數據

logs 括號 指定位置 當前 class 區域 pre 否則 寫入 筆者的應用場景:將圖像指定區域的像素值按其空間位置寫到 txt 文件中。 int hang = 4; // 矩陣行數 int lie = 8; // 矩陣列數 // 先在txt文件中構造一個空矩陣

指定世界指定位置的Block轉化為箱子

tst state bsp color 所有 位置 material tty lac 在bukkit中,block可以操作所有的三位像素方塊,如果是向對block進一步操作,我們就需要得到BlockState, BlockState表示一個方塊的狀態,才能夠對方塊進行位置等

mysql欄位指定位置插入字串

update  security_user set birthday=  CONCAT(left(birthday,4),'-',right(birthday,LENGTH(birthday)-4))  where LENGTH(birthday)=8 ; updat

JS 在陣列指定位置插入/刪除資料的方法

splice() 方法向/從陣列中新增/刪除專案,然後返回被刪除的專案。 語法: arrayObject.splice(index,howmany,item1,.....,itemX) 引數說明 引數 描述