如何从javascript中的文本框中输出字符串?

时间:2018-08-11 19:39:59

标签: javascript html

我正在研究pyg-llatin转换器,但是在此阶段我要做的就是将p标记内的内容文本更改为在文本框中键入的内容,但是当我按下按钮时,什么也没发生。

HTML:

var word

function translateWord() {
  getWord();
  outputWord();
  alert("test");
}


function getWord() {
  word = Document.getElementById("wordIn").value
}


function outputWord() {
  Document.getElementById("wordOut").innerHTML = word;
}
<!doctype html>

<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Pig Latin Translator</title>



</head>

<body>
  <script src="JS/Translator.js"></script>
  <h1>Pig Latin Translator</h1>
  <br>
  <form>
    <input type="text" id="wordIn">
    <button type="button" name="Translate" onclick="translateWord()">Translate</button>
    <p id="wordOut">-</p>

  </form>


  <br>

</body>

</html>

3 个答案:

答案 0 :(得分:0)

Documentimport { mount } from 'enzyme'; import * as React from 'react'; import { App } from './App'; describe('App', () => { it ('Should display loading until data arrives', async () => { const inputControl = mount(<App messageid={1} />); expect(inputControl.html()).toBe('<span>Loading...</span>'); // Let the event loop cycle so the callback queued by 'then' // in 'componentDidMount()' has a chance to execute await Promise.resolve(); expect(inputControl.html()).toBe('<div>The message: Message 1</div>'); }); }); 的实例的类,但是它们并不相同。

document

更改后,您的代码将起作用:

console.log(document === Document) // => false
console.log(document instanceof Document) // => true
var word

function translateWord() {
    getWord();
    outputWord();
    alert("test");
}


function getWord()
{
   word = document.getElementById("wordIn").value
}


function outputWord()
{
    document.getElementById("wordOut").innerHTML = word;
}

答案 1 :(得分:0)

将您的translationWord()函数替换为:

function translateWord() {
  document.getElementById("wordOut").innerHTML = document.getElementById("wordIn").value;
}

答案 2 :(得分:0)

var word

function translateWord() {
    getWord();
    outputWord();
    alert("test");
}


function getWord()
{
   word = document.getElementById("wordIn").value
}


function outputWord()
{

    document.getElementById("wordOut").innerHTML = word;
}