我想通過 javascript 從 Internet Explorer 的背景關系選單中操作 DOM。為此,我在注冊表中創建了以下鍵:
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\MenuExt\Remove Node]
@="javascript:var d=document,h=d.head,b=d.body,css='<style>tr{border:solid 1px red;}</style>';h.innerHTML.indexOf(css)==-1?h.innerHTML =css:h.innerHTML=h.innerHTML.replace(css,'');b.addEventListener('mousedown',function(c){var t=c.target;if(c.ctrlKey){c.preventDefault();t.style.display='none';}});void(0);"
"Contexts"=dword:00000001
但它不適用于以下錯誤:“檔案參考未定義或為空”。然后我嘗試了更簡單的方法,例如正確顯示為“某物”的 alert('something')。我還嘗試了更簡單的 DOM 參考,例如alert(document.title)回傳空。
如何從 Internet Explorer 的背景關系選單訪問 DOM 元素?
提前致謝。
uj5u.com熱心網友回復:
好的,我通過使用外部 html 檔案而不是 javascript URI 自己解決了問題。如果有人需要它,他們應該這樣做:
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\MenuExt\Remove Node]
@="c:\path\to\the_html_file.html"
"Contexts"=dword:00000001
和 the_html_file.html 檔案:
<html>
<script language="javascript" defer>
var docment=window.external.menuArguments.document;
// now it works!
alert(docment.title);
</script>
</html>
有關更多資訊:將條目添加到標準背景關系選單
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/512485.html
上一篇:如何解決wxWebView(wxWidgets)中的“確保網址//ieframe.dll/dnserrordiagoff.htm#正確”錯誤
