我有以下脚本,在 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;
}

有人知道更好的解决方案吗?

有帮助吗?

解决方案

我想我发现了问题。这可能是一个 jQuery bug,但我不知道......我将发布另一个问题来寻求帮助。

所以我通过添加一个字符串找到了解决这个问题的方法。由于某种原因,IE 中的 jQuery 不会附加对象。所以这有效:

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).append(d);
}

编辑: Pointy 解决了我的问题 另一个问题. 。事实证明这不是一个错误,但 IE 不允许您附加在窗口外部创建的对象。他的解决方案如下:

window.opener.$('<div/>', {
 'class': 'highlight',
 css: {
  position:   'absolute',
  height:     '50px',
  width:      '50px',
  left:       '200px',
  top:        '200px',
  background: '#fff',
  opacity:    0.5,
  zIndex:     99
 }
}

但请确保 window.opener 使用 jQuery v1.4 或更高版本。

其他提示

您没有打开它

var w = window.open(...);
w.document.open(); //Open the document up
w.document.write(highlight);
w.document.close();

埃里克

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top