1. 程式人生 > >14.2.1選擇文本、取得選擇的文本、選擇部分文本

14.2.1選擇文本、取得選擇的文本、選擇部分文本

oct mouse 默認事件 文本 沒有 start mit amp 標準

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Document</title>
</head>
<body>
<p id=‘p‘>test<p>
<form> 
    <input type=‘submit‘ value=‘Submit Form‘ >
</form>
<form>  
    <button type=‘submit‘ name="submit-btn" autofocus>Submit Form</button>
</form>
<form name=‘form2‘>
    <input type=‘image‘ src=‘2.jpg‘ width=‘15%‘height=‘15%‘>
</form>
<form>
    <input type=‘reset‘ value=‘Rest From‘>
    <button type=‘reset‘>Rest From</button>
</form>
<form>
    <select>
      <option value ="volvo">Volvo</option>
      <option value ="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>
</form>
<form>
    <!-- 
        行間添加JavaScript代碼,解決IE下mouseout不能取消焦點問題
        標準下獲得焦點可以select()取消焦點blur()
        非標準下獲得焦點select()取消焦點 
this.value = this.value --> <input id="txtId" type="text" value="over out" onmouseover="javascript:this.select();" onmouseout="javascript:this.blur()||(this.value=this.value);"/> </form> <form> <textarea rows=‘15‘ cols=‘15‘ style=‘resize:none‘>121545sd2154521212312313213</textarea> </form> <script type="text/javascript"> var
EventUtil = { addHandler: function(element,type,handler){//添加事件 if (element.addEventListener) { element.addEventListener(type,handler,false); }else if (element.attachEvent) { element.attachEvent(
‘on‘+type,function(){ //做的IE兼容 handler.call(element); }); }else { element[‘on‘+type] = handler; } }, getEvent: function(event){//獲得事件對象 return event || window.event; }, getTarget: function(event){//獲得事件元素 return event.target || event.srcElement; }, preventDefault: function(){//取消默認事件行為 if (event.preventDefault) { event.preventDefault(); }else { event.returnValue = false; } }, removeHandler: function(element,type,handler){//取消事件 if (element.removeEventListener) { element.removeEventListener(type,handler,false) }else if (element.dettchEvent) { element.dettchEvent(‘on‘+type,handler); }else { element[‘on‘+type] = null; } }, stopPropagation: function(event){//取消冒泡機制 if (event.stopPropagation) { event.stopPropagation(); }else { event.cancleBubble = true; } }, getRelatedTarget: function(event){ if (event.relatedTarget) { return event.relatedTarget;//標準下返回相關元素 }else if (event.toElement) { return event.toElement;//mouseout事件觸發,保存相關元素 }else if (event.fromElement) { return event.fromElement;//mouseover事件觸發,保存相關元素 } }, getButton: function(event){//鼠標按鈕兼容 if (document.implementation.hasFeature(‘MouseEvents‘,‘2.0‘))//標準下 { return event.button; }else { switch (event.button)//非標準下 { case 0: case 1: case 3: case 5: case 7: return 0; case 2: case 6: return 2; case 4: return 1; } } }, getWheelDelta: function(event){//滾輪事件兼容 //所以要兼容,寫兩個函數函數 //client的兼容性必須先寫出來 if (event.wheelDelta) { /* 兼容opear9.5以前版本的正負相反,mousewheel */ return (window.client.engine.opera && window.client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta); }else { /* 兼容firefox正負和3的倍數的問題,DOMMouseScroll */ return -event.detail*40; } }, getCharCode: function(event){//鍵盤事件兼容 if (typeof event.charCode == ‘number‘)//首先檢測按鍵有沒有代表的字符,如果沒有就沒有charCode,為undefined { return event.charCode; }else { return event.keyCode } } }; var af = document.forms; /* function sub(event){ event = EventUtil.getEvent(event); var target = EventUtil.getTarget(event); //target.elements獲得是一個集合可以用下表取得也可以用名字獲得 var btn = target.elements[‘submit-btn‘]; document.body.innerHTML += btn ; //禁用提交 btn.disable = true; } EventUtil.addHandler(af[1],‘submit‘,sub) //刷新網頁設置聚焦的元素 //如果input元素type屬性為hidden或css的display和visibility屬性隱藏了該字段,同樣也會導致錯誤 EventUtil.addHandler(window,‘load‘,function(event){ document.forms[1].elements[‘submit-btn‘].focus(); }); //元素可以添加autofocus來自動聚焦,如果添加了ele.autofocus === true //檢測是否設置了autofocus,autofocus是布爾值 EventUtil.addHandler(window,‘load‘,function(event){ var element = document.forms[1].elements[‘submit-btn‘]; if (element.autofocus !== true) { element.focus(); console.log( ‘JS focus‘ ); } }); //獲得焦點,然後文本框變色,或者觸發change事件,檢測輸入的內容是否合法 var textBox = document.forms[document.forms.length-1].elements[0]; console.log( textBox ); EventUtil.addHandler(textBox,‘focus‘,change1); EventUtil.addHandler(textBox,‘blur‘,change2); EventUtil.addHandler(textBox,‘change‘,change3); function change1(event){ event = EventUtil.getEvent(event); var target = EventUtil.getTarget(event); if (target.style.background === ‘‘) { target.style.background = ‘yellow‘; } } function change2(event){ event = EventUtil.getEvent(event); var target = EventUtil.getTarget(event); if (/[^\d]/.test(target.value)) { target.style.background = ‘red‘; }else { target.style.background = ‘‘; } } function change3(event){ event = EventUtil.getEvent(event); var target = EventUtil.getTarget(event); console.log(1) if (/[^\d]/.test(target.value)) { target.style.background = ‘red‘; }else { target.style.background = ‘‘; } } */ /* //非標準下聚焦的觸發事件 var textBox = af[af.length-1].elements[0]; EventUtil.addHandler(textBox,‘focus‘,change); function change(event){ event = EventUtil.getEvent(event); var target = EventUtil.getTarget(event); target.select(); } */ //這裏不能使用select觸發,雖然標準下可以正常,但是IE兼容的不行他會在剛一觸發就執行 //所以只能用mouseup觸發 var textBox = af[af.length-1].elements[0]; EventUtil.addHandler(textBox,‘mouseup‘,fn); function fn(event){ var that = this; //ie兼容 var a = compa(that); alert(a); } //兼容函數 function compa(that){ if (typeof that.selectionStart === ‘number‘) { return that.value.substring(that.selectionStart,that.selectionEnd); }else { //document.selection.createRange()為一個對象 return document.selection.createRange().text; } } alert(blur in window) </script> </body> </html>

14.2.1選擇文本、取得選擇的文本、選擇部分文本