Created
August 26, 2011 05:08
-
-
Save tango238/1172748 to your computer and use it in GitHub Desktop.
[HTML5]Text Selection
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Text Selection</title> | |
<style> | |
textarea { | |
width: 600px; | |
height: 150px; | |
border: 3px solid #cccccc; | |
padding: 5px; | |
background-position: bottom right; | |
background-repeat: no-repeat; | |
} | |
</style> | |
<body> | |
<section id="wrapper"> | |
<header> | |
<h1>Text Selection</h1> | |
</header> | |
<article> | |
<textarea id="content" readonly>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea> | |
</article> | |
</section> | |
Selected Text: <output id="selected"></output> | |
<script> | |
(function(){ | |
var content = document.getElementById('content'); | |
var out = document.getElementById('selected'); | |
// マウスアップ時に実行される | |
content.onmouseup = function(event){ | |
// デフォルトの動作が実行されないようにする | |
event.preventDefault(); | |
// 選択範囲を取得する | |
var selected = content.value.substring(content.selectionStart, content.selectionEnd); | |
// output要素に書き出す | |
out.value = selected; | |
}; | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment