我使用setSelectionRange有什么问题?

时间:2016-02-26 21:42:29

标签: javascript html

div中的文字未被选中。我究竟做错了什么?

要进行测试,请双击div,输入一些文字,然后点击离开。 Example on CodePen

<script>
  function editIt(div){
    // console.log(div.textContent);  
    div.contentEditable=true
    div.focus()
  }

  function selectIt(div){
    // div.focus
    div.setSelectionRange(2, 2)
    div.focus()
  }
</script>

  <div ondblclick="editIt(this)" style="border:1px solid; width:100px; height:20px" onblur="selectIt(this)" />

THX!

1 个答案:

答案 0 :(得分:1)

bracketsGrob <- function(...){ l <- list(...) e <- new.env() e$l <- l grid:::recordGrob( { do.call(grid.brackets, l) }, e) } # note that units here are "npc", the only unit (besides physical units) that makes sense # when annotating the plot panel in ggplot2 (since we have no access to # native units) b1 <- bracketsGrob(0.33, 0.05, 0, 0.05, h=0.05, lwd=2, col="red") b2 <- bracketsGrob(1, 0.05, 0.66, 0.05, h=0.05, lwd=2, col="red") p <- the_plot + annotation_custom(b1)+ annotation_custom(b2) + scale_y_continuous(expand=c(0.11,0)) p ggsave("test.png", p, width = 4, height = 2.5) 仅适用于setSelectionRange元素,您似乎正在使用input。查看 docs

当我用输入元素替换它时,请参阅 working example