我如何将javascript变量设置为textarea标签的内容?

问题描述 投票:0回答:3

我目前正在尝试将我的javascript变量设置为在第一个<textarea>标记中键入的任何内容,但是它不起作用,并且由于不具备javascript的丰富经验,因此我不知道如何解决它。任何人的任何帮助,我们将不胜感激。我还没有尝试过其他任何东西,因为我想不起要尝试的任何东西。

var t =  document.getElementById("text").value
function func() {
    document.getElementById("ascii").value = t;
}
.con {
    display: flex; 
    margin-top: 2px;
    margin-left: 20px;
}

.button {
    background: #4CAF50;
    border: none;
    outline: none;
    color: #ffffff;
    padding: 14px;
    height: 60px;
    width: 140px;
    border-radius: 0 10px;
    margin-top: 0px;
    font-size: 22px;
    cursor: pointer;
}

.txt {
    display: flex; 
    margin-right: 20px;
    background: #ffffff;
    border: 0;
    outline: none;
    height: 700px;
    width: 45%;
    border-radius: 10px;
    box-shadow: 0 4px 8px 0 rgba(141, 105, 105, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    margin-top: 0px;
}

.text {
    border: none;
    margin-top: 18px;
    margin-left: 18px;
    height: 660px;
    width: 630px;
    outline: none;
    font-size: 22px;
    resize: none;
}

.asci {
    background: #ffffff;
    border: 0;
    outline: none;
    height: 700px;
    width: 45%;
    border-radius: 10px;
    box-shadow: 0 4px 8px 0 rgba(141, 105, 105, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.ascii {
    border: none;
    margin-top: 20px;
    margin-left: 10px;
    height: 660px;
    width: 640px;
    outline: none;
    font-size: 22px;
    resize: none;
}
<html>
<head>
    <title>alphabetical order machine</title>
    <link rel="stylesheet" href="ascii.css">

</head>
<body>
    <div class="con">
        <!--button onclick="func()">button</button-->
    <form class="txt">
        <textarea class="text" id="text" type="text" placeholder="type your text here!"></textarea>        
        <input class="button" type='button' value="alphabetize" onclick="func()">
    </form>
    <form class="asci">
        <textarea class="ascii" id="ascii" type="text"></textarea>
    </form>
    </div>
    <script src="ascii.js"></script>
</body>
</html>
javascript html css
3个回答
0
投票

将您的var声明放入函数中:

function func() {
   var t =  document.getElementById("text").value
   document.getElementById("ascii").value = t;
}

0
投票

我不确定这是否是您想要做的,只是告诉我。

onblur="update()"添加到textarea class="text",以便在退出第一个文本区域时,无论是按Enter还是在其外部单击,都将调用update()函数,而第二个输入将采用第一个文本的值。


0
投票
var t =  document.getElementById("text").value
function func() {
    document.getElementById("ascii").value = t;
}

当前您的var t = document.getElementById("text").value代码将只执行一次。并且第一次,该值将为“”,因为textArea第一次为空。

© www.soinside.com 2019 - 2024. All rights reserved.