html5 Selection,Range對象

你是什麼東西發生的法師打發士大夫

你是什麼東西發生的法師打發士大夫

<script> //=================================== // //selection對象:每個窗口都具有一個Selection對象 //range對象:每個Selection對象包含多個range對象,代表一個選取的區域 // // //=================================== //獲得窗口的selection對象 var selection=document.getSelection(); var p1=document.getElementById('p1'); var p2=document.getElementById('p2'); function clickDown1(){ //range對象的數量 console.log(selection.rangeCount); //獲得range對象 console.log(selection.getRangeAt(0).toString()); } function clickDown2(){ //創造一個空的range對象 var newRange=document.createRange(); //爲range對象選擇一個節點 newRange.selectNode(p2); //爲range對象選擇一個節點包含的內容 //newRange.selectNodeContents(p2); //刪除元素或元素中的內容 newRange.deleteContents(); } function clickDown3(){ //創造一個空的range對象 var newRange=document.createRange(); //爲range對象選擇一個節點包含的內容的開始位置 newRange.setStart(p2.firstChild,2); //爲range對象選擇一個節點包含的內容的結束位置 newRange.setEnd(p2.firstChild,5); //刪除元素或元素中的內容 newRange.deleteContents(); } function clickDown4(){ //刪除所有的range對象 selection.removeAllRanges(); } function clickDown5(){ var newRange=document.createRange(); newRange.selectNodeContents(p2); selection.addRange(newRange); } </script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章