עבר עריכה לאחרונה בתאריך 30.11.09 בשעה 17:15:34 על-ידי Ice Cold (מנהל הפורום)
אני רוצה ליצור דף שבו יש div עם טקסט בתוכו וכאשר אני מסמן קטע מסוים ולוחץ על כפתור אני רוצה שהקטע טקסט הזה בלבד יודגש, לא מצאתי פתרון שפועל גם בexploer וגם בfirefox!הנה מה שהצלחתי להגיע
<HTML> <HEAD> <TITLE>Getting Selected Text</TITLE> <SCRIPT LANGUAGE="JavaScript"> var isNav4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) == 4) var isNav4Min = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 4) var isIE4Min = (navigator.appName.indexOf("Microsoft") != -1 && parseInt(navigator.appVersion) >= 4) function getPosStart(element) { var element = document.getElementById(element); // The current selection var range = document.selection.createRange(); // We'll use this as a 'dummy' var stored_range = range.duplicate(); // Select all text stored_range.moveToElementText( element); // Now move 'dummy' end point to end point of original range stored_range.setEndPoint( 'EndToEnd', range ); // Now we can calculate start and end points element.selectionStart = stored_range.htmlText.length - range.htmlText.length; return (element.selectionStart); } function getPosEnd(element) { var element = document.getElementById(element); // The current selection var range = document.selection.createRange(); // We'll use this as a 'dummy' var stored_range = range.duplicate(); // Select all text stored_range.moveToElementText(element); // Now move 'dummy' end point to end point of original range stored_range.setEndPoint( 'EndToEnd', range ); // Now we can calculate start and end points element.selectionEnd = element.selectionStart + range.htmlText.length; return (element.selectionEnd); } function showSelection() { var theSelectedText,beforeTheSelectedText,afterTheSelectedText,theNewString; if (isNav4Min) { var posStart = getPosStart('selectedText'); var posEnd = getPosEnd('selectedText'); theSelectedText = document.getElementById('selectedText').innerHTML.substr(posStart,posEnd-posStart); theSelectedText = theSelectedText.replace(theSelectedText,theSelectedText.bold()); beforeTheSelectedText = document.getElementById('selectedText').innerHTML.substr(0,posStart); afterTheSelectedText = document.getElementById('selectedText').innerHTML.substr(posEnd); theNewString=beforeTheSelectedText.concat(theSelectedText,afterTheSelectedText); document.getElementById('selectedText').innerHTML = theNewString; } else if (isIE4Min) { if (document.selection) { var posStart = getPosStart('selectedText'); var posEnd = getPosEnd('selectedText'); theSelectedText = document.getElementById('selectedText').innerHTML.substr(posStart,posEnd-posStart); theSelectedText = theSelectedText.replace(theSelectedText,theSelectedText.bold()); beforeTheSelectedText = document.getElementById('selectedText').innerHTML.substr(0,posStart); afterTheSelectedText = document.getElementById('selectedText').innerHTML.substr(posEnd); theNewString=beforeTheSelectedText.concat(theSelectedText,afterTheSelectedText); document.getElementById('selectedText').innerHTML = theNewString; event.cancelBubble = true } } temp=''; temp2=''; } if (isNav4) { document.captureEvents(Event.MOUSEUP) } </SCRIPT> </HEAD> <BODY> <div id="selectedText" style='border: 1px solid #000000; height: 100px;' contenteditable=true>Select some text and see how JavaScript can capture the selection:</div> <a href='#' onclick='showSelection()'>בולד</a> </BODY> </HTML>
|
https://img329.imageshack.us/img3
29/4057/dorgalamvx7.jpg