tmp
Thursday, March 13, 2014
選択範囲の取得
JavaScriptで選択範囲の取得をするのには `window.getSelection` か `document.selection` を使用して ```javascript function getSelectionText(){ if (window.getSelection) { return window.getSelection().toString(); } else if (document.selection) { return document.selection.createRange().text; } return null; } ``` とすれば良いが IE10,11とFF?では対象のelementがinput=text, textareaの場合にwindow.getSelectionで取得できないので考慮する (IE10ならdocument.selectionがまだ使えるのでそれで取得してもいいけど微妙) ```javascript function getSelectionText(){ if (window.getSelection && document.activeElement){ var userSelection; if (document.activeElement.nodeName == "TEXTAREA" || (document.activeElement.nodeName == "INPUT" && document.activeElement.getAttribute("type").toLowerCase() == "text")){ var ta = document.activeElement; userSelection = ta.value.substring(ta.selectionStart, ta.selectionEnd); } else { userSelection = window.getSelection(); } return userSelection.toString(); } else if (window.getSelection) { return window.getSelection().toString(); } else if (document.selection) { return document.selection.createRange().text; } return null; } ``` ###参考 * http://forums.mozillazine.org/viewtopic.php?f=25&t=2268557
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment