簡體   English   中英

無法獲取未定義或空引用的屬性“createRange”

[英]Unable to get property 'createRange' of undefined or null reference

以下代碼在我升級到Windows 8.1 / Internet Explorer 11之前一直運行良好,現在拋出錯誤:“無法獲取未定義或空引用的屬性'createRange'”

var SelectedData = window.external.menuArguments.document.selection.createRange().text;

有沒有修復/解決這個問題?

*問題更新如下更新的代碼仍然無效....

<html><head><title>-</title><script type="text/JScript">
function Launch()
{
var TheSelection = document.getSelection();
if(TheSelection != null)
{

.... do  a bunch of stuff

}
window.close();
}
</script></head><body onload="Launch();" </body></html>

我也嘗試過window.getselection; window.getselection(); 。window.getselection()的ToString();

這些似乎都不起作用...... ???

document.selection的document.selection在右上角說:

不再支持選擇。 從Internet Explorer 11開始,使用getSelection。 有關信息,請參閱兼容性更改。

document.selection.createRange().text更改為document.getSelection()

問題正是我所預測的。 您在null或未定義的引用上調用createRange() 具體來說, document.selection未定義。 錯誤消息確切地說錯了。

這真的不是很多上下文,但一般來說,你的錯誤信息意味着你沒有做到這一點:

var SelectedData;
var selection = window.external.menuArguments.document.selection;
if(selection != null)
{
  SelectedData = selection.createRange().text;
}

當您嘗試進行選擇時,未進行任何選擇,因此選擇為空。 當對象為null時,您無法查詢它,因為包含所需信息的結構不存在。

對於此調整,您可以找到:

b=document.selection.getSelection()

或類似的東西然后你可以使用下面的代碼:

b=typeof document.selection!=="undefined"?document.selection.getSelection():null

暫無
暫無

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

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