1. 程式人生 > >div contenteditable游標位置問題

div contenteditable游標位置問題

<span style="color:#ff0000;">插入資料後,游標位置問題</span>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>div contenteditable游標測試</title>
<style type="text/css">
	.div{ width:600px; height:200px; border:1px solid #CCC}
</style>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
	$(document).ready(function(e) {
        $('.aclick').click(function(){
			$('.div-label').html("回覆[瀟sdfsdfssssssssssssssdfsdf瀟]:");
			$('.div').focus();
			getC($('.div'))
		})
    });
	function getC(that){
			if(document.all){
				that.range=document.selection.createRange();
				that.range.select();
				that.range.moveStart("character",-1); 
			}else{
				that.range=window.getSelection().getRangeAt(0);
				that.range.setStart(that.range.startContainer,2);
			}
	}
</script>
</head>

<body>
	<a href="#" class="aclick">回覆瀟瀟</a>
	<div class="div" contenteditable="true">
    	<label contenteditable="false" class="div-label">回覆標題:</label>
    </div>
</body>
</html>