簡體   English   中英

range.select在IE10上無法正確突出顯示文本

[英]range.select fails on IE10 to properly highlight text

我有一個應用程序,它遍歷文本區域,搜索特定的文本,如果找到則突出顯示,並提示用戶是否對找到的文本采取行動。 在IE10之前的所有IE版本上,此方法均能正常工作(突出顯示文本,並提示用戶)。

對於IE10,由於顯示為CONFIRM提示,所以range.select將不再突出顯示文本。 如果刪除了CONFIRM,則文本將突出顯示。 同樣在IE 10上,在確定最后一個確認后,當表單返回焦點時,文本的最后一個實例將突出顯示。

下面是一個代碼片段,它將演示這一點,並且將在IE 5、6、7、8、9下正常運行,但在IE10上將失敗。 在重新設計或其他代碼替代方面的任何幫助將不勝感激。 我只需要在IE環境(內部應用程序,僅限於IE使用)中工作即可。


<html>
<body>

<form name="theForm">
   <textarea name="message" rows="20" cols="80">NAM/JONES  NAM/SMITH   NAM/BROWN</textarea>
</form>

<script>
   var message = document.theForm.message.value.toUpperCase();
   var range = document.theForm.message.createTextRange();

   for (x=0; x<message.length-1; x++) {
      if (message.substr(x,4) == "NAM/") {
         var strFound=range.findText("NAM/");
         if (strFound) range.select();
         range.collapse(false);
         msg = "Process this name?";
         if (confirm(msg)) {
            // use x.substr to extract name, and do something
         }
      }
   }
</script>

</body>
</html>

我遇到了類似的問題,並根據您的問題和我們有問題的代碼制作了一個演示

<?xml version="1.0" encoding="ISO-8859-1" ?>
<html>
<head>
    <title></title>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
</head>
<body>

    <form name="theForm">
        <input type="text" id="txt" value="1234567890" />
        <span id="selected" />
    </form>

    <script>
        function selectRange(start, end) {
            var range = document.theForm.txt.createTextRange();
            range.moveStart("character", 0 - end);
            range.moveEnd("character", 0 - end);
            range.select();
            // Und dann den neu hinzugekommenen Teil markieren.
            range.moveStart("character", start);
            range.moveEnd("character", end - start);
            range.select();
        }
        function step(x) {
            if (x >= document.theForm.txt.value.length - 2)
                return;
            selectRange(x, x + 2);
            document.getElementById("selected").innerHTML = document.selection.createRange().text;
            window.setTimeout(function () { step(x + 1); }, 1000);
        }
        step(0);
    </script>

</body>
</html>

原來是那個range.select(); 當它發生在setTimeout()時,它將再次工作。

我們最終將有問題的功能完全放入了settimeout

this.selectTextRange = function (start, end) {
    window.setTimeout(function() {
        var range = thisControl.textBox.createTextRange();
        // Ganz an den Anfang moven     
        range.moveStart("character", 0 - end);
        range.moveEnd("character", 0 - end);
        range.select();
        // Und dann den neu hinzugekommenen Teil markieren.
        range.moveStart("character", start);
        range.moveEnd("character", end - start);
        range.select();
    }, 0);
};

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM