日期:2014-05-16 浏览次数:20480 次
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>test pasteHTML</title>
<meta content="text/html; charset=UTF-8" />
<script>
function test(){
var sel = document.selection;
if (sel!=null) {
var rng = sel.createRange();
if (rng!=null) {
rng.pasteHTML("<p><strong>Selection has been REPLACED.</strong></p>");
}
}
}
</script>
</head>
<body>
<textarea id="test" rows="20" cols="100"></textarea>
<br />
<input type="button" value="replace" onclick="test();" />
<script>
var sel = document.selection;
//var sel = document.getElementById("test").document.selection;
if (sel!=null) {
var rng = sel.createRange();
alert(rng.text);
if (rng!=null) {
document.getElementById("test").focus();
rng.pasteHTML("<p><strong>Selection has been replaced.</strong></p>");
}
}
</script>
</body>
</html>