Skip to content

Instantly share code, notes, and snippets.

@foru17
Created February 19, 2014 08:05
Show Gist options
  • Save foru17/9087869 to your computer and use it in GitHub Desktop.
Save foru17/9087869 to your computer and use it in GitHub Desktop.
模仿新浪微博插入话题 自动选中
<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