HTMLTextAreaElement: selectionchange ã¤ãã³ã
Baseline
2024
Newly available
Since September 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
selectionchange ã¯é¸æ API ã®ã¤ãã³ãã§ã<textarea> è¦ç´ å
ã®ããã¹ãã®é¸æç¶æ
ã夿´ãããã¨ãã«çºçãã¾ãã
ããã¯ãæåã®é¸æç¯å²ã®å¤æ´ã¨ããã£ã¬ãããç§»åããå ´åã®ä¸¡æ¹ãå«ã¿ã¾ãã
ãã®ã¤ãã³ãã¯ãã£ã³ã»ã«ä¸å¯ã§ãã
ã¤ãã³ãã¯é常 <textarea> ã«ã¤ãã³ããªã¹ãã¼ã追å ãããã¨ã§å¦çãããHTMLTextAreaElement ã§èªã¿è¾¼ã¾ãããã³ãã©ã¼é¢æ°ã§å¦çããã¾ããselectionStartãselectionEndãselectionDirection ããããã£ã§èªã¿åãã¾ãã
ã°ãã¼ãã«ãª onselectionchange ã¤ãã³ããã³ãã©ã¼ã«ãªã¹ãã¼ã追å ãããã³ãã©ã¼é¢æ°å
ã§ Document.getSelection() ã使ç¨ã㦠Selection ãåå¾ãããã¨ãå¯è½ã§ãããããããã㯠ããã¹ã ã®é¸æç¯å²ã®å¤æ´ãåå¾ããã®ã«ã¯ãã¾ãæçã§ã¯ããã¾ããã
æ§æ
ãã®ã¤ãã³ãåã addEventListener() ãªã©ã®ã¡ã½ããã§ä½¿ç¨ããããã¤ãã³ããã³ãã©ã¼ããããã£ãè¨å®ããããã¦ãã ããã
addEventListener("selectionchange", (event) => { })
onselectionchange = (event) => { }
ã¤ãã³ãå
ä¸è¬ç㪠Event ã§ãã
ä¾
ä¸è¨ã®ä¾ã§ã¯ã<textarea> è¦ç´ å
ã§ã®é¸æç¯å²ãåå¾ããæ¹æ³ãç´¹ä»ãã¾ãã
HTML
<div>
ããã«ããã¹ããå
¥åãã¦é¸æãã¦ãã ãã:<br /><textarea
id="my-text"
rows="2"
cols="20"></textarea>
</div>
<div>selectionStart: <span id="start"></span></div>
<div>selectionEnd: <span id="end"></span></div>
<div>selectionDirection: <span id="direction"></span></div>
JavaScript
const myInput = document.getElementById("my-text");
myInput.addEventListener("selectionchange", () => {
document.getElementById("start").textContent = myInput.selectionStart;
document.getElementById("end").textContent = myInput.selectionEnd;
document.getElementById("direction").textContent = myInput.selectionDirection;
});
ä¾
仿§æ¸
| Specification |
|---|
| Selection API> # selectionchange-event> |
| Selection API> # dom-globaleventhandlers-onselectionchange> |