【问题标题】:document.queryCommandValue returns (empty string) in Firefoxdocument.queryCommandValue 在 Firefox 中返回(空字符串)
【发布时间】:2014-01-06 09:17:48
【问题描述】:

我想知道 contenteditable 元素中的某个文本是否为粗体。在 Chrome 中 document.queryCommandValue("bold") 返回 "true"/"false" 作为字符串,IE 返回 true/false 作为布尔值,但 Firefox 在开发者控制台中返回 (empty string)

我以小提琴为例: http://jsfiddle.net/nTQd2/

如果您在 div 中写入 som 文本,请将其标记,然后点击“粗体”,跨度应显示 "true"/"false"true/false。我真的不在乎它是字符串还是布尔值,因为我可以稍后将其转换。

【问题讨论】:

    标签: javascript html firefox contenteditable


    【解决方案1】:

    对于粗体、斜体和类似字体,请使用document.queryCommandState()。对非布尔命令使用document.queryCommandValue()

    http://jsfiddle.net/nTQd2/1/

    【讨论】:

      【解决方案2】:

      这很简单。

      Document.queryCommandValue() 
      

      返回一个字符串。而不是使用这个。

      Document.queryCommandState('Bold')
      

      这将返回一个布尔值作为真/假。

      例子:

      function isSelectedTextBold()
      {
          var isBold = document.queryCommandState("Bold");
          return isBold;
      }
      

      我还为其他格式选项指定函数调用。

      1. 斜体 -> document.queryCommandState("Italic");
      2. 下划线 -> document.queryCommandState("Underline");
      3. 左对齐 -> document.queryCommandState("justifyLeft");
      4. 居中对齐 -> document.queryCommandState("justifyCenter");
      5. 右对齐 -> document.queryCommandState("justifyRight");

      我希望这会有所帮助。 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多