简体   繁体   English

打开弹出窗口时加载代码

[英]Load code when I open a popup

I need to load some code JS when I load a popup window. 加载弹出窗口时,需要加载一些代码JS。 I've tried with: 我尝试过:

mypopup.ready(function...

But nothing. 但是什么都没有。

Here's my code. 这是我的代码。

  function popup(){
   mypopup = window.open('http://33bits.es/foro/masiconos.php', 'popup', 'width=400','height=300','status=no', 'scrollbars=yes', 'toolbars=no', 'menubar=no', 'location=no');

}

 $('#mas').click(function(evt) { popup();  });

mypopup.ready(function() {


//Más Iconos
$('img').click(function (evt) {
   var clase =  $(this).attr('class');
   insertar_popup(clase);
});
$('.cerrar').click(function(evt) { window.close(); });


//Mas Iconos CSS
$('.cerrar').css("font-family", "Arial");
$('.cerrar').css("cursor", "pointer");
$('.cerrar').css("margin", "50%");
$('.pop').css("background", "url(http://www.33bits.es/foro/Themes/epic_2_0/images/id/abgg.png)");
$('.iconos_pop > img').css("margin", "5px");
$('.iconos_pop > img').css("cursor", "pointer");
$('.iconos_pop').css("border-radius", "10px");
$('.iconos_pop').css("padding", "10px");
$('.iconos_pop').css("background", "white");
$('.iconos_pop').css("margin", "5% auto");
$('.iconos_pop').css("width", "95%");
});

I want to load the popup and then, load this part of my code, it will give style to the popup. 我想加载弹出窗口,然后加载代码的这一部分,它将为弹出窗口赋予样式。 But it doesn't load and my popup doesn't have style, it seems empty and shabby. 但是它没有加载,我的弹出窗口没有样式,它看起来空荡荡的。

A potential solution to your problem could be the use of jQuery Dialogs . 一个可能的解决方案是使用jQuery Dialogs It appears the code that you want to load styles the dialog on open, but using jQuery Dialogs allows you to style it with CSS directly. 在打开时,您似乎要加载样式化对话框的代码,但是使用jQuery Dialogs可以直接使用CSS对其进行样式设置。 Follow the link below for examples, as well as source code for initializing it. 请点击以下链接获取示例,以及用于对其进行初始化的源代码。

http://jqueryui.com/dialog http://jqueryui.com/dialog

The basic setup uses jQuery notation, you create a in your html page and add a "dialog" class to it: 基本设置使用jQuery表示法,您在html页面中创建一个,并向其中添加“对话框”类:

$(function(){
    $("#yourDialog").dialog();
})

Now to have it open on click of a link, you will need to add some options to the code so that it does not open by default and only when you click on a button that activates it: 现在要在单击链接时打开它,您将需要在代码中添加一些选项,以使其默认情况下不会打开,只有在单击激活它的按钮时才打开:

Javascript Java脚本

$(function(){
    $("#yourDialog").dialog({
        autoOpen: false 
    });

    $("#yourButton").click(function(){
       $("#yourDialog").dialog("open");
    });
})

Html HTML

<div id="yourDialog" title="Basic dialog">
    <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

<button id="yourButton">Button</button>

For a more complete example, visit the link http://jqueryui.com/dialog/#modal-form . 有关更完整的示例,请访问链接http://jqueryui.com/dialog/#modal-form

Because you're modifying attribues of DOM elements in your main page, not the popup. 因为您正在修改主页中的DOM元素的属性,而不是弹出窗口。

Moreover, mypopup variable isn't set when you attempt to call its ready method. 此外,当您尝试调用它的ready方法时,未设置mypopup变量。 You could try the following: 您可以尝试以下方法:

function popup() {
  var mypopup = window.open('http://33bits.es/foro/masiconos.php', 'popup', 'width=400', 'height=300', 'status=no', 'scrollbars=yes', 'toolbars=no', 'menubar=no', 'location=no');

  mypopup.onload = function() {
      var doc = mypopup.document,
          $popDocument = $(doc);

      // Everything you want
      $popDocument.find('.popClass').click(function() {
        alert('Hey you clicked in the popup!');
      });
  };
}

$('#mas').click(popup);

See it in action: http://plnkr.co/edit/u4YvcFtL1aUiVdmCMVhU?p=preview 实际操作中查看它: http : //plnkr.co/edit/u4YvcFtL1aUiVdmCMVhU?p=preview

Note that you should not manipulate your popup's contents by doing like $('selector') because it will search in you main page. 请注意,您不应像$('selector')那样操作弹出窗口的内容,因为它将在您的主页中进行搜索。 Instead in the example above, do $popDocument.find('selector') . 而是在上面的示例中,执行$popDocument.find('selector')

Here is an answer that wraps the popup in an object in a nice way: Get DOM elements of a popup for jQuery manipulation 这是一个很好的将弹出窗口包装在对象中的答案: 获取jQuery操作弹出窗口的DOM元素

That's becaus you can not edit the styling of another webpage that your request in your popup. 那是因为您无法在弹出窗口中编辑您所请求的另一个网页的样式。 That would be a security issue if it where possible. 如果可能的话,那将是一个安全问题。

if " http://33bits.es/foro/masiconos.php " is one of your own pages you could edit that and use 如果“ http://33bits.es/foro/masiconos.php ”是您自己的页面之一,则可以对其进行编辑和使用

document.ready to do the styling document.ready做样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM