IE的问题并附加到window.opener [英] Problems with IE and appending to the window.opener
问题描述
我有以下脚本在Firefox和Chrome中运行良好(不确定其他浏览器),但它在IE中根本不起作用。它基本上打开一个弹出窗口,附加一个div来突出显示窗口开启器中的项目。我希望它在同一个站点上的多个页面上工作,所以我不想在主窗口(window.opener)中添加一个函数来创建div。抱歉,我无法发布工作演示 - window.opener无法在bin中工作。
I have the following script that works well in Firefox and Chrome (not sure about other browsers), but it doesn't work at all in IE. It basically opens a popup which appends a div to highlight an item in the window opener. I want it to work across multiple pages on the same site, so I didn't want to add a function to create the div in the main window (window.opener). Sorry I couldn't post a working demo - window.opener doesn't work in a bin.
<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但没有成功。我最终发现这种方法有效,但它是一个可怕的解决方案,导致页面闪烁。
I have also tried to use appendChild without success. I ultimately found this method to work, but it is a horrible solution and causes the page to blink.
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;
}
任何人都知道更好的解决方案吗?
Anyone know of a better solution?
推荐答案
我想我发现了这个问题。它可能是一个jQuery错误,但我不知道...我会发布另一个问题来获得帮助。
I think I found the problem. It might be a jQuery bug, but I can't tell... I'll post another question to get help.
所以我找到了解决这个问题的方法只是追加一个字符串。出于某种原因,IE中的jQuery不会附加对象。所以这样做:
So I found the solution to this problem by just appending a string. For some reason jQuery in IE won't append an object. So this works:
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不允许你追加在窗口外创建的对象。他的解决方案如下:
Pointy solved my problem in another question. It turns out it's not a bug, but IE not allowing you to append an object created outside of the window. His solutions is as follows:
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或更高版本。
But make sure the window.opener is using jQuery v1.4 or greater.
这篇关于IE的问题并附加到window.opener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!