简体   繁体   English

Javascript-toUpperCase在新窗口中

[英]Javascript - toUpperCase in new window

This is how I want the code to be. 这就是我想要的代码。 When you write any word it will automatically change to uppercase using onkeyup. 当您写任何单词时,它将使用onkeyup自动变为大写。 Also when user clicks on new window, the uppercase letters will be shown in the new window. 同样,当用户单击新窗口时,大写字母将显示在新窗口中。 I can only do the first part. 我只能做第一部分。 How do I do this? 我该怎么做呢? https://jsfiddle.net/p3zea7Lu/11/ https://jsfiddle.net/p3zea7Lu/11/

<body>
  <div>
    <form>
      <br /><br /> Type in your text: <br />
      <p>
        <input type="text" size="30" id="input" onkeyup="convertToUppercase()" />
      </p>
      <p id="output"></p>
    </form>

    <br />
    <button onclick="myFunction()">Open new window</button>

  </div>
  <script>
    function convertToUppercase() {
      document.getElementById('output').innerHTML = document.getElementById('input').value.toUpperCase();
    }

    function myFunction() {
      var myWindow = window.open("", "MsgWindow", "width=200,height=100");
      myWindow.document.write("The word is:" + input);
    }
  </script>
</body>

You can store the input value in a separate variable and then pass it to the document.write 您可以将输入值存储在单独的变量中,然后将其传递给document.write

An example fiddle: https://jsfiddle.net/zgbrxk2c/ 小提琴的例子: https : //jsfiddle.net/zgbrxk2c/

Change your myFunction to below. myFunction更改为以下内容。

function myFunction(val) {
        var myWindow = window.open("", "MsgWindow", "width=200,height=100");
        myWindow.document.write("The word is:" + document.getElementById('output').innerHTML);
    }

Here is your updated code. 这是您更新的代码。

 <html> <head> <meta charset="utf-8" /> <style> </style> </head> <body> <div> <form> <br /><br /> Type in your text: <br /> <p> <input type="text" size="30" id="input" onkeyup="convertToUppercase()" /> </p> <p id="output"></p> </form><br /> <button onclick="myFunction()">Open new window</button> </div> <script> function convertToUppercase() { document.getElementById('output').innerHTML = document.getElementById('input').value.toUpperCase(); } function myFunction(val) { var val = document.getElementById("output").textContent; var myWindow = window.open("", "MsgWindow", "width=200,height=100"); myWindow.document.write("The word is:" + val); } </script> </body> </html> 

<button onclick="myFunction(document.getElementById('output').innerHTML)">
    Open new window
</button>
function myFunction(val) {
    var myWindow = window.open("", "MsgWindow", "width=200,height=100");
    myWindow.document.write("The word is:" + val);
}

Hope this helps you. 希望这对您有所帮助。

 function myFunction(val) {
            var myWindow = window.open("", "MsgWindow", "width=200,height=100");
            myWindow.document.write("The word is:" +  document.getElementById('output').innerHTML);
        }

// Just replaced input with document.getElementById('output').innerHTML //只是将输入替换为document.getElementById('output')。innerHTML

Are popups enabled on that PC's Chrome? 该PC的Chrome是否启用了弹出式窗口? If they're not then the new window cannot be created hence win is undefined 如果不是,则无法创建新窗口,因此undefined win

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

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