Created
July 3, 2013 17:55
-
-
Save 7cc/5921006 to your computer and use it in GitHub Desktop.
for MDN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
make btn#selectPre | |
click -> select pre-content | |
remake - if webkit support mouseenter | |
*/ | |
;(function(d){ | |
if(!window.getSelection){ | |
return | |
} | |
var btn = d.createElement("button") | |
btn.id = "selectPre" | |
btn.textContent = "select" | |
btn.addEventListener("click", selectPre, false) | |
function selectPre(){ | |
window.getSelection().selectAllChildren(this.parentNode) | |
} | |
var pres = d.getElementsByTagName("pre") | |
for(var i=pres.length; i--;){ | |
pres[i].addEventListener("mouseover", addBtn, false) | |
//pres[i].addEventListener("mouseout", remBtn, false) // if mouseleave | |
} | |
function addBtn(e){ | |
if(this === addBtn.ele) return // not to addBtn if already | |
//console.log(e.target, e.currentTarget,e.relatedTarget) | |
this.appendChild(btn) | |
return addBtn.ele = this | |
} | |
/* | |
function remBtn(){ | |
btn.parentNode.removeChild(btn) | |
} | |
*/ | |
})(document) | |
/* | |
CSS | |
pre{ | |
position: relative | |
} | |
#selectPre{ | |
position: absolute; | |
top: 0; | |
right: 0; | |
border: none; | |
padding: 2px 3px; | |
font-family: consolas | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment