在窗口中不工作的onclick事件

onlick event not working within a window.open

本文关键字:onclick 事件 工作 窗口      更新时间:2023-09-26

我试图实现一个onclick事件一次窗口。Open已经打开了。这样,当用户点击新窗口中的按钮时,它就会工作。

如果onclick事件不在新打开的窗口内,则该事件有效。

<div class="show-dialog" id="content">                                 
    <script type="text/javascript"> 
      var c = document.getElementById("content"); 
      function resizeText(multiplier) { if (c.style.fontSize == "") {c.style.fontSize = "1.0em"; } c.style.fontSize = parseFloat(c.style.fontSize) + (multiplier * 0.2) + "em"; } 
    </script>
    <a href="javascript:void(0);" onclick="resizeText(1)" id="plustext">Make text bigger</a>
    <a href="javascript:void(0);" onclick="resizeText(-1)" id="minustext">Make text smaller</a>
</div>

javascript函数允许点击链接和增加或减少文本。开放。窗口函数打开新的窗口,这是好的,工作很好,但在我的窗口。打开示例,onlick事件不触发。所以我不能使用函数:(

<span class="Show"><a href="#">Show<i class="fa fa-external-link-square fa-left"></i></a></span>
$(".Show a").click(function() {
 var e = $(this).parent().next("div.show-dialog").html();
 var t = window.open("", "mywindow1", "width=950,height=550,scrollbars=yes,toolbar=yes,menubar=yes");
t.document.write("<html><head>");
t.document.write("<style>body{font-size:2em;}</style>");
t.document.write("<script type='text/javascript'>
var c = document.getElementById('content');
function resizeText(multiplier) {
  if (c.style.fontSize == '2em')
  { c.style.fontSize = '2em'; } c.style.fontSize = parseFloat(c.style.fontSize) + (multiplier * 0.2) + 'em';
}
</script>");
t.document.write("</head><body>");
$(t.document).find("body").html(e);
t.document.write("<a href='javascript:void(0);' onclick='resizeText(1)' id='plustext'>Make text bigger</a>
<a href='javascript:void(0);' onclick='resizeText(-1)' id='minustext'>Make text smaller</a>");
t.document.write("</body>");
t.document.write("</html");});

最后我设法让onclick甚至在window.open.

我还集成了一个函数来增加和减少窗口内的字体大小。开放的onclick。

// Creating window to open for text that are in div with class name show-dialog
$(".Show a").click(function() {
    // Varible grabs content of show dialogs
    var activityContent =  $(this).parent().next("div.show-dialog").html();
    // Establish varibale to create buttons to increase text
    var userGUI = "<div class='userGUI'><a href='javascript:void(0);' onclick='resizeText(1)' id='plustext'><i class='fa fa-plus-square-o fa-2x'></i></a>
    <a href='javascript:void(0);' onclick='resizeText(-1)' id='minustext'><i class='fa fa-minus-square-o fa-2x'></i></a><br>Increase / Decrease font size.</div>";
    // Created variable for the window.open
    var inWindow = window.open("", "mywindow1", "width=950,height=550,scrollbars=yes,toolbar=yes,menubar=yes");
    // Writing to the inWindow variable
    inWindow.document.write("<html><head>");
    inWindow.document.write("<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css'><link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>");
    inWindow.document.write("<title>Hello world</title>");
    inWindow.document.write("<style>body{font-family: 'Open Sans', sans-serif; font-size: 18px; line-height: 1.8;}a{color:#663366;}a:hover{color:#844484;}.userGUI{float:right;}</style>");
    inWindow.document.write("</head><body id='content'>");
    $(inWindow.document).find("body").append(userGUI);
    $(inWindow.document).find("body").append("<br><br>");
    $(inWindow.document).find("body").append(activityContent);
    inWindow.document.write("</body><script type='text/javascript'>
    // Increase font size function
    function resizeText(multiplier) {
      var c = document.getElementById('content');
      if (c.style.fontSize == '')
      { c.style.fontSize = '1.175em'; } c.style.fontSize = parseFloat(c.style.fontSize) + (multiplier * 0.2) + 'em';
    }</script></html>");
    inWindow.document.write("");
    inWindow.document.close();
});

这个的html是在我的问题上面^.