代码之家  ›  专栏  ›  技术社区  ›  Mottie

IE和附加到window.opener的问题

  •  1
  • Mottie  · 技术社区  · 16 年前

    <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;
    }
    

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

    2 回复  |  直到 16 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    我想我发现了问题所在。这可能是一个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);
    }
    

    编辑: 波蒂用英语解决了我的问题 another question . 事实证明,这不是一个bug,但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使用的是jqueryv1.4或更高版本。

        2
  •  0
  •   epascarello    16 年前

    你没有打开它

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

    埃里克