简体   繁体   English

如何在新打开的弹出屏幕上执行javascript / jquery

[英]How to execute javascript / jquery on a newly opened popup screen

So basically I have jquery code on a page where: 所以基本上我在页面上有jquery代码,其中:

  1. Open a new popup window 打开一个新的弹出窗口
  2. Display rails view page 显示rails视图页面
  3. Manipulate items on a newly opened page 操纵新打开页面上的项目

Don't know what sort of solutions are there, however I thought it should be really easy. 不知道有什么样的解决方案,但我认为它应该很容易。

That's the code: 那是代码:

// open a popup window for example /fault_books/3
popup = window.open("/fault_books/" + <%= @fault_book.id %> , "popup");

// trying to get the scope of the element
var module = $(".module-logo", popup.document.body)

// manipulating the element
$(module).hide();

Not sure it will be cross-browser, but you may try something like : 不确定它是否会跨浏览器,但您可以尝试以下方法:

var popup = window.open("/fault_books/" + <%= @fault_book.id %> , "popup");
$(popup.document).ready(function(){
  var module = $(".module-logo", $(popup.document))
  // manipulating the element
  $(module).hide();
});

Why don't you just put your javascript code to the generated response from "/fault_books/" + <%= @fault_book.id %>" 你为什么不把你的javascript代码放到"/fault_books/" + <%= @fault_book.id %>"生成的响应中"/fault_books/" + <%= @fault_book.id %>"

In that case, just do it as normal: 在这种情况下,只需照常进行:

$(document).ready(function(){
    var module = $(".module-logo")

    // manipulating the element
    $(module).hide();
});

And don't need to do anything when opening a new window, just call: 打开新窗口时不需要做任何事情,只需致电:

popup = window.open("/fault_books/" + <%= @fault_book.id %> , "popup");

This solution is simple and easier to maintain as scripts are self-contained. 由于脚本是自包含的,因此该解决方案简单且易于维护。 Scripts on a page do jobs only for the containing page. 页面上的脚本仅为包含页面执行作业。 Let's say, if you have another page opening the same url window.open("/fault_books/" + <%= @fault_book.id %> , "popup"); 假设你有另一个页面打开相同的url window.open("/fault_books/" + <%= @fault_book.id %> , "popup"); . You don't have to duplicate code. 您不必重复代码。

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

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