text()の引数であるコードの内容が分かりにくいです。
自分なりに調べてある程度は理解出来ていると思うのですが、
自信がないので添削して頂けないでしょうか。
よろしくお願い致します。
「改訂新版jQuery本格入門 P231 リスト6-8」のコードより。一部編集を加えています。
jQuery
1document.selection && 2 document.selection.createRange().text || 3 $(this) 4 .val() 5 .slice(this.selectionStart, this.selectionEnd)]
【追記・正しくコードを理解出来ているか不安な部分】
(論理和演算子の左辺が真の場合)
document.selection && document.selection.createRange().text
↓
Selectionオブジェクトが使えるならdocument.selection.createRange()によりRangeオブジェクトを取得し、
textプロパティから文字列を取得する。IEに対応しているかを問うコード。
(論理和演算子の左辺が偽の場合)
$(this).val()よりテキストエリアからテキストを取得し、
.slice(this.selectionStart, this.selectionEnd)で選択範囲を指定した上で文字列を生成する。
jQuery
1 const techr = $('#tech-r'); // ラジオボタンの選択(change) 2 const techt = $('#tech-t'); // テキストの選択(select) 3 4 // <form onsubmit="return false;">と同じ(フォームを送信しない) 5 $('form').submit(function() { return false; }); 6 7 // ラジオブタンの選択が変更されたら 8 $('form input[type="radio"]').change(function() { 9 techr.text(this.value); // 選択されたラジオボタンの値を表示 10 }); 11 12 // テキストが選択されたら 13 $('form textarea').select(function() { 14 techt 15 .text(document.selection && // @@@@@@@@ 以下からが疑問のコード @@@@@@@ 16 document.selection.createRange().text || 17 $(this) 18 .val() 19 .slice(this.selectionStart, this.selectionEnd) 20 ); 21 });
HTML
1 <form> 2 <fieldset> 3 <legend>Web関連技術</legend> 4 <input id="tech-h" name="tech" type="radio" value="HTML"> 5 <label for="tech-h">HTML</label> 6 <input id="tech-c" name="tech" type="radio" value="CSS"> 7 <label for="tech-c">CSS</label> 8 <input id="tech-j" name="tech" type="radio" value="JavaScript"> 9 <label for="tech-j">JavaScript</label> 10 <input id="tech-f" name="tech" type="radio" value="Flash"> 11 <label for="tech-f">Flash</label> 12 </fieldset> 13 <fieldset> 14 <legend>jQueryについて</legend> 15 <textarea name="tech" cols="30" rows="3"> 16 jQueryはHTML文書に組み込んで利用されるJavaScriptライブラリです 17 </textarea> 18 </fieldset> 19 <input type="submit" value="送信"> 20 </form> 21 <table> 22 <tr> 23 <td>ラジオボタンの選択(change)</td> 24 <td id="tech-r"></td> 25 </tr> 26 <tr> 27 <td>テキストの選択(select)</td> 28 <td id="tech-t" style="width: 10em;"></td> 29 </tr> 30 </table> 31 32

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。