主要代码:
function AttachEvent(obj, eventName, handler, argsObject) {
var eventHandler = handler; if (argsObject) { eventHandler = function (e) { handler.call(this, argsObject); } } if (window.attachEvent)//IE obj.attachEvent("on" + eventName, eventHandler); else//FF obj.addEventListener(eventName, eventHandler, false); }用监听器绑定具体的方法与对应的处理函数。
当然也可以直接用JQuery的绑定。
实例:
<script type="text/javascript">
var SWord; var IDS; function onReady() { IDS = { txtAlarmID: '<%= txtAlarmID.ClientID %>', txtLogicType: '<%= txtLogicType.ClientID %>', ddlDeviceType: '<%= ddlDeviceType.ClientID %>', textSolution: '<%= textSolution.ClientID %>', txtLogicType: '<%= txtLogicType.ClientID %>' }; SWord = { text: '', Start: 0, End: 0 }; var textareaSo = document.getElementById(IDS.textSolution); AttachEvent(textareaSo, "click", clickSelectedText, IDS.textSolution); var textLogicType = document.getElementById(IDS.txtLogicType); AttachEvent(textLogicType, "click", clickSelectedText, IDS.txtLogicType); }; function AttachEvent(obj, eventName, handler, argsObject) { var eventHandler = handler; if (argsObject) { eventHandler = function (e) { handler.call(this, argsObject); } } if (window.attachEvent)//IE obj.attachEvent("on" + eventName, eventHandler); else//FF obj.addEventListener(eventName, eventHandler, false); } function clickSelectedText(Id) { var o = document.getElementById(Id); if (document.selection) { var k = document.selection.createRange().text; if (k.lenght > 0) { } } else { var p1 = o.selectionStart; var p2 = o.selectionEnd; if (p1 || p1 == '0') { if (p1 != p2) {//选中 SWord.Start = p1; SWord.End = p2; SWord.word = o.value.substring(p1, p2); } else //点击 { if (p1 > SWord.Start && p2 < SWord.End) { document.getElementById("Div1").innerHTML = SWord.word; } SWord.Start = 0; SWord.End = 0; SWord.word = ''; } } } }本人声明:沐海(http://my.oschina.net/mahaisong) 以上文章是经过本人设计实践和阅读其他文档得出。如果需要探讨或指教可以留言!欢迎交流!