如何在Javascript中将两个函数作为单个函数?

问题描述 投票:-2回答:3

我有这两个功能:

function myFunction() {
    var x = document.getElementById("myTextarea").value;
    document.getElementById("demo").innerHTML = x;
}

function myFunction1() {
    var x = document.getElementById("myTextarea1").value;
    document.getElementById("demo1").innerHTML = x;
}

我没有成功将它们合并为一个函数。

javascript
3个回答
7
投票
function myFunction(inputId,targetId) {
        var x = document.getElementById(inputId).value;
        document.getElementById(targetId).innerHTML = x;
}
myFunction('myTextarea','demo');
myFunction('myTextarea1','demo1');

1
投票

你的意思是:

function myFunction() {
    var textArea = document.getElementById("myTextarea").value;
    var textArea1 = document.getElementById("myTextarea1").value;
    document.getElementById("demo").innerHTML = textArea;
    document.getElementById("demo1").innerHTML = textArea1;
}

0
投票

insertValue('Value','Inner')这是一个获得2个参数的函数,first-value id和second inner id。您可以输入任何ID并使用此多用途功能

    function insertValue(id_value, id_inner) {
      let x = document.getElementById(id_value).value;
      document.getElementById(id_inner).innerHTML = x;
    }

    insertValue('Value','Inner');
<input  id="Value" type="" name="" value="value which you want to put">
 <div id="Inner">inner text which you will dont see</div>
© www.soinside.com 2019 - 2024. All rights reserved.