js可以做出这种点击a标签向textarea插入标签并且把光标放在标签之间的效果吗?

https://img2.mukewang.com/5c304e44000169bf06610343.jpg

上面动态图可以看出来,当点击粗体的时候,就会向textarea插入标签对,并且光标自动出现在标签中间!其它的也是一样,这么怎么做?

泛舟湖上清波郎朗
浏览 1101回答 1
1回答

慕村9548890

<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title>Hello World</title>&nbsp; &nbsp; &nbsp; &nbsp; <meta http-equiv="content-type" content="text/html; charset=utf-8">&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <input type="button" onclick="test()" value="a">&nbsp; &nbsp; &nbsp; &nbsp; <textarea id="txt" cols="30" rows="10"></textarea>&nbsp; &nbsp; </body>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; var txt = document.getElementById("txt");&nbsp; &nbsp; &nbsp; &nbsp; txt.value = "123456";&nbsp; &nbsp; &nbsp; &nbsp; function test(value){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var temp = txt.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt.focus();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var pointIndex = txt.selectionStart;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var str1 = temp.substr(0,pointIndex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var str2 = temp.substr(pointIndex,temp.length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt.value = str1+"<a></a>"+str2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newIndex = pointIndex+3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt.setSelectionRange(newIndex,newIndex);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript