JavaScript_查詢當前選定的文本樣本;

來源:互聯網
上載者:User

<html>
    <head>
        <title>查詢當前選定的文本樣本</title>
        <script type="text/javascript">
            function getSelectedText() { // 擷取使用者選定的文本;
                var result = "";
                if(window.getSelection) {
                    result = window.getSelection().toString();
                    result += "\n _window.getSelection()";
                } else if(document.getSelection) {
                    result = document.getSelection();
                    result += "\n _document.getSelection()";
                } else if(document.selection) {
                    result = document.selection.createRange().text;
                    result += "\n _document.selection.createRange().text";
                }
                if(window.addEventListener) {
                    console.log(result);
                }else if(window.attachEvent) {
                    alert(result);
                }
            }

            // 文本輸入元素處理
            function getTextFieldSelection(e) {
                if(e.selectionStart != undefined && e.selectionEnd != undefined) {
                    var start = e.selectionStart;
                    console.log("start_" + start);
                    var end = e.selectionEnd;
                    console.log("end_" + end);
                    var strOut = e.value.substring(start, end);
                    console.log(strOut);
                } else {
                    console.log("Not supported on this browser.");
                }
            }
        </script>
    </head>
    <body>
        <p>我愛我,老婆!</p><br>
        <a href="javascript:
            var q;
            if(window.getSelection) {
                q = window.getSelection().toString();
            }else if(document.getSelection) {
                q = document.getSelection();    
            }else if(document.selection) {
                q = document.selection.createRange().text;
            }
            void window.open('http://zh.wikipedia.org/wiki/' + q);
            
        ">在維基百科中尋找所選定的文本資訊</a>
        <button onclick="getSelectedText()">擷取選定文本</button><br>
        <label for="textname">看看是否能擷取裡邊的文本:</label>
        <input type="text" name="textname" value = "i love you!" size="23" />

        <p><b>相容性問題說明:</b>如果選定的文本在一個input或textarea表單元素中,Window和Document對象的getSelection()方法不會返回它,
        它們只能從文檔自身的內容中返回選擇的文本。但是IE的document.selection屬性則會從文檔中的任何地方返回選擇的文本。</p>

        <label for="filename">文本輸入元素的處理:</label>
        <input type="text" name="filename" size="23" /><input type="button" value="元素處理" onclick = "getTextFieldSelection(event)" />
    </body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.