我有以下脚本在 Firefox 和 Chrome 中运行良好(不确定其他浏览器),但它在 IE 中根本不起作用。它基本上会打开一个弹出窗口,该弹出窗口附加一个 div 以突出显示窗口打开器中的项目。我希望它可以在同一个站点上的多个页面上工作,所以我不想添加一个函数来在主窗口(window.opener)中创建 div。抱歉,我无法发布工作演示 - window.opener 在垃圾箱中不起作用。
<button>Open popup</button>
<script type="text/javascript">
$(document).ready(function(){
$(':button').click(function(){
var highlight = "" +
"<button>Click to Add Highlight</button>" +
"<scr"+"ipt type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js'></scr"+"ipt>" +
" <scr"+"ipt type='text/javascript'>" +
" $(':button').click(function(){" +
" $('<div/>', {" +
" 'class': 'highlight'," +
" css: {" +
" position: 'absolute'," +
" height: '50px'," +
" width: '50px'," +
" left: '200px'," +
" top: '200px'," +
" background: '#fff'," +
" opacity: 0.5," +
" zIndex: 99" +
" }" +
" }).appendTo( $(window.opener.document.body) );" +
" })" +
" </scr"+"ipt>";
var w = window.open('','highlighter','toolbar=0,location=0,status=0,width=200,height=100,scrollbars=1,resizable=1');
w.document.write(highlight);
w.document.close();
})
})
</script>
我也尝试过使用 appendChild 但没有成功。我最终发现这种方法有效,但它是一个可怕的解决方案,并导致页面闪烁。
if ($.browser.msie){
var d = '<div class="highlight" style="position:absolute;height:50px;' +
'width:50px;left:200px;top:200px;background:#fff;opacity:0.5;' +
'filter:alpha(opacity=50);zIndex:99;"></div>';
window.opener.document.body.innerHTML = window.opener.document.body.innerHTML + d;
}
有人知道更好的解决方案吗?