[英]Get DOM elements of a popup for jQuery manipulation
I am creating a popup as follows: 我正在创建一个弹出窗口如下:
var comet = {
popup: null,
newPopup: function(windowsname, w, h){
this.popup = window.open(windowsname, windowsname, 'width=' + w + ',height=' + h);
var self = comet;
var logOut= null;
$(this.popup.document).ready(function(){
logOut = self.popup.document.getElementById('logout');
console.log(logOut);
$(logOut).live('click', function(){
alert('HELLO');
return false;
})
})
},
some_function: function(){
//calling it here:
this.newPopup('index.php',1120,550);
}
}
The logOut
sometimes (usually on the 1st window open) returns null. logOut
有时(通常在第一个窗口打开)返回null。 Also, the click handler never goes through and the original click handler operates. 此外,单击处理程序永远不会通过,原始单击处理程序也会运行。
How do I overwrite the real click handler of the popup? 如何覆盖弹出窗口的真实点击处理程序?
Both pages are on my site, so there should be no cross site issues.. 这两个页面都在我的网站上,所以应该没有跨站点问题..
Here is a fiddle that shows a little of what I am trying to do: http://jsfiddle.net/maniator/K2B3q/ 这是一个小提琴,展示了我想要做的一些事情: http : //jsfiddle.net/maniator/K2B3q/
Tested and working in Firefox 4 and Chrome 11. Check it out. 在Firefox 4和Chrome 11中经过测试和使用。 请查看。
$(function()
{
var comet =
{
popup: null,
newPopup: function(url, w, h)
{
this.popup = window.open(url, url, 'width=' + w + ',height=' + h);
var self = this;
this.popup.onload = function ()
{
var doc = this.document,
script = doc.createElement('script');
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js';
script.onload = function ()
{
function setup()
{
$('#logout').click(function () { alert('It worked!'); });
}
script = doc.createElement('script');
script.textContent = "(" + setup.toString() + ")();";
doc.body.appendChild(script);
};
doc.head.appendChild(script);
};
},
foo: function()
{
this.newPopup('http://jsbin.com/amuza3/2', 600, 400);
}
};
$('#clickme').click(function ()
{
comet.foo();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.