我使用这个简单的Javascript代码来检索在网页上选择的文本:
var userselection = window.getSelection(); //user selection will be a Selection-Object
var rangeObject = userselection.getRangeAt(0); 
Run Code Online (Sandbox Code Playgroud)
如果选择文本,代码工作正常.如果我只是单击可以选择的文本,我也会得到一个范围对象,它会按预期折叠.
但如果我不选择也不点击,Firefox会抛出此错误:
uncaught exception: [Exception... "Component returned failure code: 0x80070057
(NS_ERROR_ILLEGAL_VALUE)  [nsISelection.getRangeAt]" nsresult: "0x80070057 
(NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: file:///home/jan/Webprojekte/test-range
selection.html# :: who :: line 16" data: no]
Run Code Online (Sandbox Code Playgroud)
是否有一种干净的方法来防止错误但仍然通知用户没有选择文本?
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           5212 次  |  
        
|   最近记录:  |