2

更新:根据下面的建议,这就是我想做的事情:如果我双击鼠标光标从“蓝绿色”的“b”到“n”的任何地方,我想要所有单词“蓝绿色”应突出显示。如何才能做到这一点?目前,根据您单击的位置,它将“蓝绿色”视为三个单独的字符串。因此,如果您在“blue”的“b”和“e”之间双击,它只会突出显示“blue”而不是“-green”。如果双击连字符,它会单独突出显示连字符。如果您在“green”的“g”和“n”之间双击,它只会突出显示“green”而不是“blue-”。

ORIGINAL:当我双击一个带连字符的单词或一组字符(例如“123-abc”或“blue-green”等)时,只有我双击的单词部分被突出显示。我想突出显示整个单词。

我正在使用 Windows 7 专业版。如果需要在每个应用程序的基础上完成,我最感兴趣的是为 Google Chrome 修复它,但任何与 Windows 兼容的网络浏览器都可以。

4

2 回答 2

1

老问题,但我碰巧一直在研究同样的问题。这是我的解决方案:

jsFiddle.net

"use strict"

// Tweak to make a double-click select words with hyphens
// 
// As of 2016-0816, None of the major Mac browser selects whole words
// with hyphens, like "ad-lib". This tweak fixes the hypen issue.
// 
// Note: Firefox 48.0 doesn't automatically select whole words with 
// apostrophes like "doesn't". This tweak also treats that.

;(function selectWholeWordsWithHyphens(){
  var pOutput = document.getElementById("output")
  var selection = window.getSelection()
  // Regex designed to find a word+hyphen before the selected word.
  // Example: ad-|lib|
  // It finds the last chunk with no non-word characters (except for
  // ' and -) before the first selected character. 
  var startRegex = /(\w+'?-?)+-$/g
  // Regex designed to find a hyphen+word after the selected word.
  // Example: |ad|-lib
  var endRegex = /^-('?-?\w+)+/
  // Edge case: check if the selection contains no word
  // characters. If so, then don't do anything to extend it.
  var edgeRegex = /\w/

  document.body.ondblclick = selectHyphenatedWords

  function selectHyphenatedWords(event) {
    if (!selection.rangeCount) {
      return
    }
    var range = selection.getRangeAt(0)
    var container = range.startContainer
    var string = container.textContent
    var selectionUpdated = false

    if (string.substring(range.startOffset, range.endOffset)
              .search(edgeRegex) < 0) {
      // There are no word characters selected
      return
    }

    extendSelectionBackBeforeHypen(string, range.startOffset)
    extendSelectionForwardAfterHyphen(string, range.endOffset)

    if (selectionUpdated) {
      selection.removeAllRanges()
      selection.addRange(range)
    }

    function extendSelectionBackBeforeHypen(string, offset) {
      var lastIndex = 0
      var result
        , index
      string = string.substring(0, offset)

      while (result = startRegex.exec(string)) {
        index = result.index
        lastIndex = startRegex.lastIndex
      }

      if (lastIndex === offset) {
        range.setStart(container, index)
        selectionUpdated = true
      }
    }

    function extendSelectionForwardAfterHyphen(string, offset) { 
      if (!offset) {
        return
      }

      string = string.substring(offset)
      var result = endRegex.exec(string)

      if (result) {
        range.setEnd(container, offset + result[0].length)
        selectionUpdated = true
      }
    }
  }
})()
于 2016-08-17T20:38:22.690 回答
-1

这是所有程序的标准,因为它们都运行操作系统的输入配置/程序。要修复它,您需要在 System32 中做一些事情。我不知道你需要做什么,但我怀疑这是你的问题。您可能应该更详细地了解您想要的具体内容。

于 2012-11-20T20:26:14.243 回答