Created
February 19, 2014 08:05
-
-
Save foru17/9087869 to your computer and use it in GitHub Desktop.
模仿新浪微博插入话题 自动选中
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<textarea id="target"></textarea> | |
<p><button id="btn">插入话题</button></p> | |
<script> | |
var $ = function(id){ | |
return document.getElementById(id); | |
}; | |
$("btn").onclick = function(){ | |
var con = "请在这里输入自定义话题"; | |
//转载文字 | |
$("target").value += "#"+con+"#"; | |
var l = $("target").value.length; | |
//创建选择区域 | |
if($("target").createTextRange){//IE浏览器 | |
var range = $("target").createTextRange(); | |
range.moveEnd("character",-l) | |
//range.moveStart("character",-l) | |
range.moveEnd("character",l-1); | |
range.moveStart("character", l-1-con.length); | |
range.select(); | |
}else{ | |
$("target").setSelectionRange(l-1-con.length,l-1); | |
$("target").focus(); | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment