簡體   English   中英

onlick事件在window.open中不起作用

[英]onlick event not working within a window.open

我試圖在window.open打開后實現onclick事件。 這樣,當用戶單擊新窗口中的按鈕時,它將起作用。

如果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功能可以單擊鏈接並增加或減少文本。 open.window函數將打開一個新窗口,該窗口很好並且可以正常工作,但是在我的window.open示例中,不會觸發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中工作。

我還集成了一個函數來增加和減小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在上面的^中是我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM