textrange

How to know if there is a link element within the selection

喜欢而已 提交于 2019-11-30 17:59:03
问题 In Javascript, I'd like determine whether an element, say an A element, exists inside a given range/textRange. The aim is to determine if the user's current selection contains a link. I am building a rich text editor control. The range object has a commonAncestorContainer (W3C) or parentElement() (Microsoft) method which returns the closest common anscestor of all elements in the range. However, looking inside this element for A elements won't work, since this common ancestor may also contain

Inserting text into an editable IFRAME at the caret position (IE)

允我心安 提交于 2019-11-28 10:30:34
I'm struggling with an actually straighforward problem: In Internet Explorer I want to insert plain text at the current caret position. This works really fine for simple TEXTAREA elements but it doesn't entirely work for editable IFRAMEs, which is what I have. In the script I use I am creating a TextRange object from the document of the IFRAME which I use to paste the text as HTML at the cursor position. <iframe id="editable"> <html> <body> Some really boring text. </body> </html> </iframe> <script type="text/javascript"> window.onload = function() { var iframe = document.getElementById(

Javascript: Move caret to last character

感情迁移 提交于 2019-11-26 22:19:27
I have a textarea and when I click in it I want to move the caret to the last character so Something[caret] function moveCaret(){ // Move caret to the last character } <textarea onclick="moveCaret();"> Something </textarea> As I know this is somehow possible with the TextRange object, but I don't really know how to use it EDIT: I would love to see only pure javascript solutions so no libraries please. The following function will work in all major browsers, for both textareas and text inputs: function moveCaretToEnd(el) { if (typeof el.selectionStart == "number") { el.selectionStart = el

Javascript: Move caret to last character

夙愿已清 提交于 2019-11-26 12:19:08
问题 I have a textarea and when I click in it I want to move the caret to the last character so Something[caret] function moveCaret(){ // Move caret to the last character } <textarea onclick=\"moveCaret();\"> Something </textarea> As I know this is somehow possible with the TextRange object, but I don\'t really know how to use it EDIT: I would love to see only pure javascript solutions so no libraries please. 回答1: The following function will work in all major browsers, for both textareas and text

Highlight text range using JavaScript

北城以北 提交于 2019-11-26 06:55:42
I would like to highlight (apply css to) a certain text range, denoted by its start and end position. This is more diffucult than it seems, since there may be other tags within the text, that need to be ignored. Example: <div>abcd<em>efg</em>hij</div> highlight(2, 6) needs to highlight "cdef " without removing the tag. I have tried already using a TextRange object, but without success. Thanks in advance! Tim Down Below is a function to set the selection to a pair of character offsets within a particular element. This is naive implementation: it does not take into account any text that may be

Highlight text range using JavaScript

守給你的承諾、 提交于 2019-11-26 01:58:08
问题 I would like to highlight (apply css to) a certain text range, denoted by its start and end position. This is more diffucult than it seems, since there may be other tags within the text, that need to be ignored. Example: <div>abcd<em>efg</em>hij</div> highlight(2, 6) needs to highlight \"cdef \" without removing the tag. I have tried already using a TextRange object, but without success. Thanks in advance! 回答1: Below is a function to set the selection to a pair of character offsets within a

Get a range&#39;s start and end offset&#39;s relative to its parent container

a 夏天 提交于 2019-11-25 23:18:03
问题 Suppose I have this HTML element: <div id=\"parent\"> Hello everyone! <a>This is my home page</a> <p>Bye!</p> </div> And the user selects \"home\" with his mouse. I want to be able to determine how many characters into #parent his selection starts (and how many characters from the end of #parent his selection ends). This should work even if he selects an HTML tag. (And I need it to work in all browsers) range.startOffset looks promising, but it is an offset relative only to the range\'s