-ããžã¥ã¢ã«ãšãã£ã¿ãŒïŒwysiwygïŒã®äœæã
-ãã©ãŠã¶ãŠã£ã³ããŠã§æ€çŽ¢ã
-BBã³ãŒãã®èšå®ã
ãªã©
ãã®èšäºã§ã¯ãèè ã¯ãããã®ãªããžã§ã¯ãã«é¢ããããã¥ã¡ã³ãã®ç¿»èš³ã1ãæã§åéããå°ããªä»éããäŸãæžããŠã¿ãŸããã 翻蚳ã¯ç¡æã§ãããéèªçã§ã¯ãããŸããããã®ãããäžæ£ç¢ºãªè¡šçŸãäžåšçšãªè¡šçŸã«åºäŒã£ãå Žåã¯ãã³ã¡ã³ããèšå ¥ããŠãã ããã
äžè¬çã«ããã®æçš¿ã¯åç §å°çšã§ããã誰ããéçºãç°¡çŽ åããããšãé¡ã£ãŠããŸãã
1.ç¯å²
ç¯å²ã¯ãããã¥ã¡ã³ãã®ãã©ã°ã¡ã³ãã«å¯Ÿå¿ãããªããžã§ã¯ãã§ããããã®ããã¥ã¡ã³ãã®ããŒãããã³ããã¹ãã®ã»ã¯ã·ã§ã³ãå«ãŸããå ŽåããããŸãã ç¯å²ãªããžã§ã¯ãã«ã€ããŠã¯ã DOM Rangeã§è©³ãã説æããŠããŸã ã
ç§ãã¡ã話ããŠããããšãç解ããããã«ãç¯å²ãªããžã§ã¯ãã®æãåçŽãªã±ãŒã¹ã«ã€ããŠèª¬æããŸããããã«ã€ããŠã¯ã以äžã§è©³çŽ°ã«èª¬æããŸãããéžæã«ã€ããŠèª¬æããŸãã 次ã®äŸã§ã¯ãæã®ããã€ãã®åèªã匷調衚瀺ããŸãã ãã®æ¹æ³ã§ãç¯å²ã«é¡äŒŒãããªããžã§ã¯ããäœæããŸãã ãã®äŸã§ã¯ãéžæããé åã®ããã¹ãã³ã³ãã³ããååŸããŸãã
äŸãžã®ãªã³ã¯
ãã ãããã®ãããªé åã¯ãã«ã¹ã¿ã éžæã䜿çšããã ãã§ãªããJavaScriptã¹ã¯ãªãããããäœæã§ããç¹å®ã®æäœãå®è¡ã§ããŸãã ãã ããç°¡åãªèª¬æçšã®ã³ãŒããäœæããŠãããã«ã¯æ©èœããŸããã ãã ããInternet Explorerã1ã€ãããŸãã Micsosoftã¯ãç¬èªã®å®è£ ã§ããTextRangeãªããžã§ã¯ããäœæããŸããã åå®è£ ãåå¥ã«åæããŸãã
1.1 Rangeã®DOMå®è£
ç¯å²ã¯ãé åã®éå§ç¹ãšçµäºç¹ã«å¯Ÿå¿ãã2ã€ã®å¢çç¹ïŒå¢çç¹ïŒã§æ§æãããŸãã å¢çç¹ã®äœçœ®ã¯ãããŒãïŒããŒãïŒãšãªãã»ããïŒãªãã»ããïŒã®2ã€ã®ããããã£ã䜿çšããŠããã¥ã¡ã³ãå ã§å®çŸ©ãããŸãã ã³ã³ããã¯ããšã³ããã€ã³ããå«ãããŒãã§ãã ã³ã³ããèªäœãšãã®ãã¹ãŠã®ç¥å ã¯ãå¢çç¹ã®èŠªã³ã³ããïŒç¥å ã³ã³ããïŒãšåŒã°ããŸãã äž¡æ¹ã®å¢çç¹ãå«ã芪ã³ã³ããã¯ãã«ãŒãã³ã³ãããšåŒã°ããŸãã
äžã®ç»åã§ã¯ãéžæå¢çãã€ã³ãã¯ã³ã³ããã§ããããã¹ãããŒãïŒïŒtext1ããã³ïŒtext2ïŒã«ãããŸãã å·Šã®å¢çç·ã®å Žåã芪ã³ã³ãããŒã¯ïŒtext1ãH1ãBODYãå³ã®å Žå-ïŒtext2ãPãBODYã§ãã äž¡æ¹ã®å¢çç¹ã®å ±éã®èŠªã¯BODYã§ããããã®èŠçŽ ã¯ã«ãŒãã³ã³ãããŒã§ãã
ã³ã³ãããããã¹ãããŒãã®å Žåããªãã»ããã¯ããŒãã®å é ããã®æåã§æ±ºå®ãããŸãã ã³ã³ãããèŠçŽ ïŒDocumentãDocumentFragmentãElement ...ïŒã®å Žåããªãã»ããã¯åããŒãã§æ±ºå®ãããŸãã
å³ãèŠãïŒ source ïŒïŒ
Rangeãªããžã§ã¯ãs1ã®å¢çç¹ã¯ããã¹ãããŒãã«ããããããªãã»ããã¯ããŒãã®å é ããã®æåã§æå®ãããŸãã s2ã®å Žåãå¢çç¹ã¯<p> Blah xyz </ p>段èœå šäœãå«ãããã«é 眮ããããããã³ã³ããã¯BODYèŠçŽ ã§ãããåããŒãã®äœçœ®ã§ãªãã»ãããèæ ®ãããŸãã
ç¯å²ãªããžã§ã¯ãã¯ãã«ãŒãã³ã³ãããŸãã¯ããã¥ã¡ã³ãã®ã³ã³ããã¹ãã§åŒã³åºãããcreareRangeã¡ãœããã䜿çšããŠäœæãããŸãã ãªããžã§ã¯ãã¯ç©ºã§äœæããããªããžã§ã¯ãã®setStartã¡ãœãããšsetEndã¡ãœããã䜿çšããŠå¢çç¹ãèšå®ãããŸãã äŸãèŠãŸãïŒ
< div id ="ex2" >
< h2 > | Range- </ h2 >
< p > | . </ p >
</ div >
$domRange = {
create : function () {
// root-
var root = document .getElementById( 'ex2' );
// ( )
var start = root.getElementsByTagName( 'h2' )[0].firstChild;
var end = root.getElementsByTagName( 'p' )[0].firstChild;
if ( root.createRange ) {
// Range
var rng = root.createRange();
// ,
rng.setStart( start, 3 );
//
rng.setEnd( end, 10 );
// ,
return rng.toString();
} else
return ', Internet Explorer, TextRange ' ;
}
}
ã¢ã¯ã·ã§ã³ã®äŸ ã
Rangeããããã£ãšã¡ãœãããç°¡åã«æ€èšããŠãã ããïŒ
- commonAncestorContainerããããã£ã¯ãæããã¹ããããã«ãŒãã³ã³ãããŒãžã®ãªã³ã¯ãè¿ããŸãã
- startContainer ïŒ endContainer ïŒããããã£ã¯ãäžéšïŒäžéšïŒå¢çç¹ã®ã³ã³ãããŒãžã®åç §ãè¿ããŸãã
- startOffset ïŒ endOffset ïŒããããã£ã¯ãäžéšïŒäžéšïŒå¢çç¹ã®ãªãã»ãããè¿ããŸãã
- collapsedããããã£ã¯ãå¢çç¹ã«åãã³ã³ãããšãªãã»ãããããå Žåã¯trueãè¿ããŸãïŒãã以å€ã®å Žåã¯falseïŒã
- setStart ïŒ setEnd ïŒã¡ãœããã¯ã察å¿ããå¢çç¹ã®ã³ã³ããïŒããŒããžã®ãªã³ã¯ïŒãšãªãã»ããïŒæŽæ°å€ïŒãèšå®ããŸãã äžèšã®äŸã
- ã¡ãœããsetStartBefore ã setStartAfter ã setEndBefore ã setEndAfterã¯ãå¯äžã®åŒæ°ãšããŠããŒãåç
§ãåããéä¿¡ãããããŒãã®èªç¶ãªå¢çã«åŸã£ãŠå¢çç¹ãèšå®ããŸãã äŸïŒ
< span id ="s1" > First </ span >
< span id ="s2" > Second </ span >
var rng = document .createRange();
// #s1
rng.setStartBefore( document .getElementById( 's1' ) );
// #s2
rng.setEndAfter( document .getElementById( 's2' ) );
- selectNodeããã³selectNodeContentsã¡ãœããã䜿çšãããšãããŒãã®å¢çã«æ²¿ã£ãŠRangeãªããžã§ã¯ããäœæã§ããŸãããããã®ãªããžã§ã¯ããžã®ãªã³ã¯ã¯ãå¯äžã®åŒæ°ãšããŠåãå
¥ããŸãã selectNodeã䜿çšãããšãéä¿¡ãããããŒããRangeã«ãªããŸãããselectNodeContentsã¯ããŒãã®ã³ã³ãã³ãããã®ã¿ãªããžã§ã¯ããäœæããŸãã
- collapseã¡ãœããã¯ãRangeãªããžã§ã¯ãã®å¢çç¹ãçµåããŸãã å¯äžã®åŒæ°ãšããŠããŒã«å€ã䜿çšããŸãïŒäžéšã§çµåããã«ã¯trueãšmdashãäžéšã§falseãšmdashïŒã ããã©ã«ãã§çã
- toStringã¡ãœããã¯ãRangeãªããžã§ã¯ãã®ããã¹ãã³ã³ãã³ããè¿ããŸãã
- cloneContentsã¡ãœããã¯ãRangeãªããžã§ã¯ãã®ã³ã³ãã³ãã®ã³ããŒãããã¥ã¡ã³ãã®ãã©ã°ã¡ã³ããšããŠè¿ããŸãã
- cloneRangeã¡ãœããã¯ãRangeãªããžã§ã¯ãèªäœã®ã³ããŒãè¿ããŸãã
- deleteContentsã¡ãœããã¯ãRangeãªããžã§ã¯ãã®ã³ã³ãã³ãå šäœãåé€ããŸãã
- detachã¡ãœããã¯ãDOMããçŸåšã®ãªããžã§ã¯ããååŸããŠãåç §ã§ããªãããã«ããŸãã
- insertNodeã¡ãœããã¯ãåäžã®åŒæ°ãšããŠããŒãïŒãŸãã¯ããã¥ã¡ã³ããã©ã°ã¡ã³ãïŒãžã®ãªã³ã¯ãåãåããéå§ç¹ã§Rangeãªããžã§ã¯ãã®ã³ã³ãã³ãã«æ¿å ¥ããŸãã
- extractContentsã¡ãœããã¯ãRangeãªããžã§ã¯ãã®ã³ã³ãã³ããåãåããåä¿¡ããããã¥ã¡ã³ãã®ãã©ã°ã¡ã³ããžã®ãªã³ã¯ãè¿ããŸãã
- SurroundContentsã¡ãœããã¯ãçŸåšã®Rangeãªããžã§ã¯ãã®ãã¹ãŠã®ã³ã³ãã³ããæ°ãã芪èŠçŽ ã«é 眮ãããã®åç §ã¯å¯äžã®åŒæ°ãšããŠäœ¿çšãããŸãã
- compareBoundaryPointsã¡ãœãã㯠ãå¢çç¹ãæ¯èŒããããã«äœ¿çšãããŸãã
ç¥èãããããŠçµ±åããããã«ãå°ããªåé¡ã解決ããŸãã ããã¹ãããŒãã§ãã¬ãŒãºãèŠã€ããéè²ã®èæ¯ã§åŒ·èª¿è¡šç€ºããŸãã
< div id ="ex3" >
""
</ div >
$domRange.highlight = function ( text ) {
//
var root = document .getElementById( 'ex3' ).firstChild;
//
var content = root.nodeValue;
//
if ( content.indexOf( text ) != -1 ) {
if ( document .createRange ) {
// , Range,
var rng = document .createRange();
// ,
rng.setStart( root, content.indexOf( text ) );
// +
rng.setEnd( root, content.indexOf( text ) + text.length );
//
var highlightDiv = document .createElement( 'span' );
highlightDiv.style.backgroundColor = 'blue' ;
// Range
rng.surroundContents( highlightDiv );
} else
alert( ', Internet Explorer, TextRange ' );
} else
alert( ' ' );
}
ã¢ã¯ã·ã§ã³ã®äŸ ã
æ®ãã®ããããã£ãšã¡ãœãããèªåã§è©ŠããŠãã ããã
MSIEã§ã®ç¯å²ã®å®è£ ã«ç§»ããŸãããã
1.2ã ããã¹ãç¯å²
MSIEå®è£ ã®TextRangeãªããžã§ã¯ãã¯ãé·ãããŒã以äžã®ããã¹ãç¯å²ã§ãã ãã®ç¯å²ã«ã¯ç¬èªã®å¢çç·ããããæŽæ°ïŒããã¹ãïŒã®æåïŒæåïŒãåèªïŒåèªïŒãæïŒæïŒã«ã移åãã§ããŸãã ã€ãŸããå¢çç·ã2ïŒ5ã8ãªã©ïŒåèªïŒã·ã³ãã«ãæïŒã ãå³ïŒå·ŠïŒã«ç§»åããŠã·ããã§ããŸãã åæã«ããªããžã§ã¯ãã«ã¯ç¯å²ã®HTMLã³ã³ãã³ãã«é¢ããããŒã¿ãæ ŒçŽãããDOMãšå¯Ÿè©±ããããã®ã¡ãœããããããŸãã
TextRangeãªããžã§ã¯ãã¯ã次ã®DOMèŠçŽ ã®ã³ã³ããã¹ãã§åŒã³åºãããšãã§ããcreateTextRangeã¡ãœããã䜿çšããŠäœæãããŸãã
BODY, BUTTON, INPUT type=button, INPUT type=hidden, INPUT type=password, INPUT type=reset, INPUT type=submit, INPUT type=text, TEXTAREA
ãã¿ã³ã䜿çšããç°¡åãªäŸïŒ
< input id ="buttonId" type ="button" value ="Test button" />
$ieTextRange = {
create : function () {
//
var button = document .getElementById( 'buttonId' );
//
if ( button.createTextRange && button.createTextRange() != undefined ) {
// TextRange
var rng = button.createTextRange();
//
return rng.text;
} else
return ', IE (!), Range ' ;
}
}
ã¢ã¯ã·ã§ã³ã®äŸ ã
TextRangeãªããžã§ã¯ãã®ããããã£ãšã¡ãœãããèæ ®ããŸã ïŒãã¹ãŠã§ã¯ãªããæãå¿ èŠãªãã®ã®ã¿ïŒã
- boundingWidth ïŒboundingHeightïŒããããã£ã¯ãTextRangeãªããžã§ã¯ããå æããå¹ ïŒé«ãïŒããã¯ã»ã«åäœã§è¿ããŸãã
- boundingTop ïŒ boundingLeft ïŒããããã£ã¯ãããã¥ã¡ã³ããŠã£ã³ããŠã«å¯Ÿãããã¹ãé åã®å·Šäžé ã®YïŒXïŒåº§æšãè¿ããŸãã
- htmlTextããããã£ã¯ããªããžã§ã¯ãã®HTMLã³ã³ãã³ããè¿ããŸãã
- textããããã£ã¯ããªããžã§ã¯ãã®ããã¹ãã³ã³ãã³ããè¿ããŸãïŒäžèšã®äŸãåç §ïŒã
- offsetTop ïŒ offsetLeft ïŒããããã£ã¯ãç¥å ã«å¯Ÿãããã¹ãé åã®å·Šäžé ã®YïŒXïŒåº§æšãè¿ããŸãã
- collapseã¡ãœããã¯ãç¯å²ã®ç«¯ç¹ãçµã¿åãããŸãã å¯äžã®åŒæ°ãšããŠããŒã«å€ã䜿çšããŸãïŒäžéšã§çµåããã«ã¯trueãšmdashãäžéšã§falseãšmdashïŒã ããã©ã«ãã§çã
- è€è£œã¡ãœããã¯ãæ¢åã®ããã¹ãç¯å²ãè€è£œã ããŸã£ããåãæ°ããç¯å²ãè¿ããŸãã
- expandã¡ãœããã¯ãçŸåšã®ããã¹ãç¯å²ãåäžã®ããã¹ãåŒæ°ãšããŠæž¡ãããããã¹ãã®åäœã«æ¡åŒµããŸãã
'character' â
'word' â
'sentence' â
'textedit' â .
æåïŒå€±æïŒã®å ŽåãtrueïŒfalseïŒãè¿ããŸãã
- findTextã¡ãœããã¯ãæåã®åŒæ°ãšããŠæž¡ãããããã¹ãæååãšäžèŽããç¯å²ã§æ€çŽ¢ããŸãïŒå€§æåãšå°æåã¯åºå¥ãããŸããïŒã äžèŽãèŠã€ãã£ãå Žåãç¯å²ã®å¢çã¯ããã«äžèŽããŸããã 2çªç®ã®ïŒãªãã·ã§ã³ã®ïŒåŒæ°ãšããŠãæ€çŽ¢ããå é ããã®æåæ°ã瀺ãæŽæ°ãæž¡ãããšãã§ããŸãã ããã«ãåŒæ°ãšããŠãå¿ èŠã«ãªãå¯èœæ§ãäœã INTãã©ã°ããªã¹ãã§ããŸãã
- åŒã³åºããæåãããšã getBookmarkã¡ãœããã¯æååãè¿ããŸãããã®æååã䜿çšããŠã moveToBookmarkã¡ãœããã䜿çšããŠããã¹ãç¯å²ã®çŸåšã®ç¶æ ã埩å ã§ããŸãã
- inRangeã¡ãœããã¯ãå¥ã®TextRangeãªããžã§ã¯ããåŒæ°ãšããŠåãåãããã®ããã¹ãç¯å²ãã³ã³ããã¹ããªããžã§ã¯ãã®ç¯å²å ã«ãããã©ããã確èªããŸãã ããŒã«å€ãè¿ããŸãã
- isEqualã¡ãœããã¯ãçŸåšã®TextRangeãªããžã§ã¯ããåŒæ°ãšããŠæž¡ããããªããžã§ã¯ããšåäžãã©ããããã§ãã¯ããŸãã ããŒã«å€ãè¿ããŸãã
- moveïŒsUnit [ãiCount]ïŒã¡ãœããã¯ãçŸåšã®ç¯å²ãé·ããŒãã«æãããã¿ãæåã®åŒæ°ïŒæå|åèª|æ| texteditïŒãšããŠæž¡ãããããã¹ãã1ã€ç§»åããŸãã 2çªç®ã®ïŒãªãã·ã§ã³ã®ïŒåŒæ°ãšããŠãç¯å²ã移åããåäœã®æ°ãæž¡ãããšãã§ããŸãã
- moveã¡ãœãããšåæ§ã«ã moveEnd ïŒ moveStart ïŒã¡ãœããã¯ãç¯å²ã®äžéšïŒäžéšïŒå¢çç·ãããã¹ãåäœã§ç§»åããŸãããã®æ°ã¯ããªãã·ã§ã³ã®2çªç®ã®ãã©ã¡ãŒã¿ãŒã§æå®ããããšãã§ããŸãã
- moveToElementTextã¡ãœããã¯ãDOMèŠçŽ ãžã®åç §ãåŒæ°ãšããŠåãåããTextRangeãªããžã§ã¯ãã®ç¯å²ã®å¢çãåä¿¡ããèŠçŽ ã®å¢çã«èšå®ããŸãã
- moveToPointã¡ãœããã¯ã2ã€ã®å¿ é åŒæ°ãšããŠãããã¥ã¡ã³ãã®å·Šäžé ãåºæºãšããXããã³Y座æšïŒãã¯ã»ã«åäœïŒãåãããã®ç¯å²ã®å¢çç·ãããã«è»¢éããŸãã
- parentElementã¡ãœããã¯ãTextRangeãªããžã§ã¯ãã®ç¯å²ïŒãŸãã¯nullïŒãå®å šã«å«ãèŠçŽ ãžã®ãªã³ã¯ãè¿ããŸãã
- pasteHTMLã¡ãœããã¯ãçŸåšã®ããã¹ãç¯å²ã®HTMLã³ã³ãã³ããåäžã®åŒæ°ãšããŠæž¡ãããæååã«çœ®ãæããŸãã
- selectã¡ãœããã¯ãTextRangeãªããžã§ã¯ãã®å 容ã«åºã¥ããŠéžæã圢æããŸããããã«ã€ããŠã¯ã以äžã§è©³çŽ°ã«èª¬æããŸãã
- setEndPointã¡ãœããã¯ãå¿ èŠãªåŒæ°ãšããŠããã¹ããã€ã³ã¿ãŒãšå¥ã®TextRangeãªããžã§ã¯ããžã®ãªã³ã¯ãåããå€ã«å¿ããŠç¯å²å¢çãã€ã³ã¿ãŒãèšå®ããŸãã ãã€ã³ã¿ã¯ããStartToEndãããStartToStartãããEndToStartãããEndToEndãã®ããããã§ãã
execCommandã¡ãœããã®ã³ãã³ãã¯ã TextRangeãªããžã§ã¯ãã«ãé©çšã§ããŸãã
çµ±åããããã«ãäžèšã®ãããªããã¹ãã³ã³ãã³ããèŠã€ããåé¡ã解決ããŸãã
< div id ="ex4" >
""
</ div >
$ieTextRange.highlight = function ( text ) {
// ,
var root = document .getElementById( 'ex4' );
//
var content = root.firstChild.nodeValue;
//
if ( content.indexOf( text ) != -1 ) {
// MSIE
if ( document .body.createTextRange ) {
// TextRange
var rng = document .body.createTextRange();
// root
rng.moveToElementText( root );
//
if ( rng.findText( text ) )
// span
rng.pasteHTML( '<span style="background:blue;">' + text + '</span>' );
} else
alert( ', IE (!), Range ' );
} else
alert( ' ' );
}
ã¢ã¯ã·ã§ã³ã®äŸ ã
æ®ãã®ããããã£ãšã¡ãœãããèªåã§è©ŠããŠãã ããã
2.éžæ
å·ŠããŠã¹ãã¿ã³ãæŒãããŸãŸã«ãŒãœã«ã移åããŠãç®çã®ãã©ã°ã¡ã³ããéžæãããšãããŒãžäžã®èŠçŽ ã®éžæã«èª°ãã粟éããŸãã ãŸãã¯ãShiftããŒãæŒããªããããŒããŒãã®ç¢å°ãã¯ãªãã¯ããŸãã ãŸãã¯ã©ãããããããããã¯åé¡ã§ã¯ãããŸããã èšäºã®ãã®éšåã§ã¯ã2ã€ã®åé¡ã解決ããæ¹æ³ãã¯ãã¹ãã©ãŠã¶ãŒã§åŠç¿ããŸããã«ã¹ã¿ã éžæãååŸããç¬èªã«èšå®ããããšã§ãã
2.1ã ã«ã¹ã¿ã éžæãååŸ
ãã®åé¡ã¯ãäŸã®èšäºã®åé ã§ããã§ã«æ··æãããŠããŸãã 次ã«ãã³ãŒããæ€èšããŸãã
$selection = {
getText : function () {
var txt = '' ;
if (txt = window.getSelection) // Not IE, getSelection
txt = window.getSelection().toString();
else // IE, selection
txt = document .selection.createRange().text;
return txt;
}
}
Internet Explorerãé€ããã¹ãŠã®ãã©ãŠã¶ãŒã¯ããŠã£ã³ããŠã³ã³ããã¹ãã§getSelectionã¡ãœããããµããŒãããŸãããã®ã¡ãœããã¯ã åè¿°ã®Rangeãšåæ§ã®ãªããžã§ã¯ããè¿ããŸãã ãã®ãªããžã§ã¯ãã«ã¯ãã¢ã³ã«ãŒãã€ã³ããšãã©ãŒã«ã¹ãã€ã³ãããããŸãã ãã€ã³ããäžèŽããããšãã§ããŸãã Selectionãªããžã§ã¯ãã®ããããã£ãšã¡ãœãããæ€èšããŠãã ããã
- anchorNodeããããã£ã¯ãéžæãå§ãŸãã³ã³ãããè¿ããŸãã éžæã®éå§ç¹ã¯ãéžæãéå§ããå¢çç·ã§ããããšã«æ³šæããŠãã ããã ã€ãŸããå³ããå·Šã«éžæãããšãå³ã®å¢çç·ãå§ãŸããŸãã ãã®ã«ãŒã«ã¯ãanchorNodeãéžæç¯å²ã®å·Šç«¯ã®ããŒããžã®ãªã³ã¯ãè¿ãOperaãã©ãŠã¶ãŒãé€ããã¹ãŠã®å Žæã§æ©èœããŸãã
- anchorOffsetããããã£ã¯ãanchorNodeã³ã³ãããŒå ã§éžæãéå§ããããã®ãªãã»ãããè¿ããŸãã
- focusNodeããããã£ãšfocusOffsetããããã£ã¯ããã©ãŒã«ã«ãã€ã³ããã€ãŸãéžæãçµäºãããã€ã³ãã«å¯ŸããŠåæ§ã«æ©èœããŸã ã ããã§ãOperaã¯ããã©ãŒã«ã¹ãã代ããã«ãéžæç¯å²ã®å³ç«¯ã®ããŒããè¿ããŸãã
- rangeCountããããã£ã¯ãçµæã®éžæã«å«ãŸããRangeãªããžã§ã¯ãã®æ°ãè¿ããŸãã ãã®ããããã£ã¯ãaddRangeã¡ãœããã䜿çšãããšãã«åœ¹ç«ã¡ãŸãã
- getRangeAtã¡ãœããã¯ãRangeãªããžã§ã¯ãã®ã€ã³ããã¯ã¹ãåŒæ°ãšããŠåãåãããªããžã§ã¯ãèªäœãè¿ããŸãã rangeCount == 1ã®å Žåã getRangeAtïŒ0ïŒã®ã¿ãæ©èœããŸãã ãããã£ãŠãçŸåšã®éžæã«å®å šã«äžèŽããRangeãªããžã§ã¯ããååŸã§ããŸãã
- collapseã¡ãœããã¯ãéžæç¯å²ããã€ã³ãïŒãã£ã¬ããïŒã«æãããã¿ãŸãã ãã£ã¬ãããé 眮ããããŒããã¡ãœããã®æåã®åŒæ°ãšããŠæž¡ãããšãã§ããŸãã
- extendã¡ãœããã¯ãåŒæ°ãšããŠã³ã³ãããšãªãã»ããïŒparentNodeãoffsetïŒãžã®åç §ãåãåããçŠç¹ããã®äœçœ®ã«ç§»åããŸãã
- collapseToStart ïŒ collapseToEnd ïŒã¡ãœããã¯ãçŠç¹ïŒåæïŒå¢çãåæïŒçŠç¹ïŒå¢çã«ç§»åãããã£ã¬ãããžã®éžæãæå°åããŸãã
- selectAllChildrenã¡ãœããã¯ãããŒããžã®ãªã³ã¯ãåäžã®åŒæ°ãšããŠåãåãããã®ãã¹ãŠã®åå«ãéžæã«è¿œå ããŸãã
- addRangeã¡ãœããã¯ãåŒæ°ãšããŠRangeãªããžã§ã¯ããåãããããéžæã«è¿œå ããŸãã ãããã£ãŠãRangeãªããžã§ã¯ãã®æ°ãå¢ããããšãã§ããŸãããã®æ°ã¯rangeCountããããã£ããããããŸãã
- removeRange ïŒ removeAllRanges ïŒã¡ãœããã¯ãæž¡ãããïŒãã¹ãŠã®ïŒRangeãªããžã§ã¯ããéžæããåé€ããŸãã
- toStringã¡ãœããã¯ãéžæç¯å²ã®ããã¹ãã³ã³ãã³ããè¿ããŸãã
Internet Explorerã¯ãéžæãšå¯Ÿè©±ããããã®ç¬èªã®ã€ã³ã¿ãŒãã§ã€ã¹ïŒããã¥ã¡ã³ãã³ã³ããã¹ãã®éžæãªããžã§ã¯ãïŒãæäŸããŸãã ãã®ãªããžã§ã¯ããæäœããã«ã¯ã次ã®ã¡ãœããã䜿çšããŸãã
- clearã¡ãœããã¯ãã³ã³ãã³ããšãšãã«éžæç¯å²ãåé€ããŸãã
- createRangeã¡ãœããïŒéèŠïŒRangeãªããžã§ã¯ããäœæããããã®createRange DOMã¡ãœãããšæ··åããªãã§ãã ããïŒïŒéžæå 容ããTextRangeãªããžã§ã¯ããäœæããŸãã
- 空ã®ã¡ãœããã¯éžæãåé€ããŸãããå 容ã¯æ®ããŸãã
éžæã®äž¡æ¹ã®å®è£ ã«ç²Ÿéããããšã§ãäžèšã®ã³ãŒããããæ確ã«ãªããŸããã
2.1ã ç¬èªã®éžæãèšå®ãã
ããŒãžäžã®ããã¹ãã®äžéšããŠãŒã¶ãŒéžæãšããŠåŒ·èª¿è¡šç€ºãããšããŸãã ããã¯ãã¯ã©ã€ã¢ã³ããããŒãžæ€çŽ¢ããã®ä»ã®ã¿ã¹ã¯ãå®è£ ããããã«å¿ èŠã§ãã ãã®åé¡ã解決ããæãç°¡åãªæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
- Rangeãªããžã§ã¯ãïŒIEã®TextRangeïŒãäœæããŸãã
- çµæã®ãªããžã§ã¯ããéžæã«å€æããŸãã
å®è£ ã確èªããŸãã
< div id ="ex5" >
< span > </ span > , .
</ div >
$selection.set = function () {
var target = document .getElementById( 'ex5' ).getElementsByTagName( 'span' )[0];
var rng, sel;
if ( document .createRange ) {
rng = document .createRange();
rng.selectNode( target )
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange( rng );
} else {
var rng = document .body.createTextRange();
rng.moveToElementText( target );
rng.select();
}
}
ã¢ã¯ã·ã§ã³ã®äŸ ã
3.ããšãã
äœæããã»ã¹äžã«æäŸãããAPIãç解ããIE DOMå®è£ ã®ç¯å²ãšéžæãæããå°ããªã©ã€ãã©ãªãäœæãããšããã¢ã€ãã¢ãæµ®äžããŸããã ãããããããååšããå Žåã§ããäœæè ã¯ãªã³ã¯ã«æè¬ããããªãèªèº«ã®æéãç¯çŽããŠãããŸãã
APIã®èª¬æã¯å®å šã§æ£ç¢ºãªãµããããŠããŸããããJavaScriptã®éçºãå¿«é©ã«è¡ãã«ã¯ãããã§ååã§ãã 以äžã®ãªã³ã¯ã§è±èªã®ãªãªãžãã«ãèŠã€ããããšãã§ããŸãã
fastcoder.orgã®ã¯ãã¹ãã¹ãèšäºRangeãTextRangeãããã³Selection ã