bbcode js 核心代码,兼容IE firefox chrome

直接上代码
在ie firefox chrome上测试通过

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
textarea 获取鼠标选中的文本<br />
<input type="button" value="code" onclick="selectText();"/>
<form action="">
	<textarea name="ta" id="ta" rows="5" cols="50"></textarea>
</form>

<script type="text/javascript">
	function selectText(){
		var oTextarea = document.getElementById('ta');
		if(document.selection){
			var text = document.selection.createRange().text;
			if(text != ""){
				document.selection.createRange().text = '[b]' + text + '[/b]';
			}
		}else if(oTextarea.selectionStart || oTextarea.selectionStart == '0'){ 
			var startP = oTextarea.selectionStart;
			var endP = oTextarea.selectionEnd;
			if(startP != endP){
				var reText = new RegExp(oTextarea.value.substring(startP, endP));
				oTextarea.value = oTextarea.value.replace(reText, '[b]' + oTextarea.value.substring(startP, endP) + '[/b]') ;
			}
		}
	}
</script>
</body>
</html>

你可能感兴趣的:(java,html,chrome,IE,firefox)