我正在尝试使用HTML和JavaScript创建锁。我想知道如何更改另一个函数的输入和输出。可以说我有一个功能
function a() {document.getElementById("x").id="b"}.
我想通过使用另一个函数来更改元素x和将要更改的id“ b”。
我尝试将函数放入另一个函数中,但没有用
function a() {document.getElementById("x").id="b";}
function c() {document.getElementById("a()").document.getElementById="b";}
答案 0 :(得分:0)
您的“其他”函数应使用arguments
调用实函数function changeId(old, new) {
document.getElementById(old).id = new;
}
function my_caller() {
changeId('x', 'b');
changeId('thisId', 'thatId');
}
答案 1 :(得分:0)
除了我的评论,您还应该使用参数-至少在函数a中。这样,函数c可以覆盖函数a设置的值。
但是,如果调用a时不带参数,则会将newId值始终设置为“ b”。
SET SESSION TIMEZONE TO 'Europe/Helsinki';
答案 2 :(得分:0)
您使代码变得复杂。简单来说,一种使用函数中的参数来解决此问题的方法。
function a(x, b) {
document.getElementById(x).id = b;
}
a("abc", "abcd");
<div id="abc"></div>
答案 3 :(得分:0)
您有两种修改现有代码的方法。
以下示例显示了稍后的操作。该示例将将a
“ x”修改为元素的函数id
转换为具有id
“ y”修改元素的同名函数。 / p>
// The old function ...
function a() {document.getElementById("x").innerHTML="b"}
// ... modifies the the x element.
a();
// Convert the function to a string, replace "x" with "y"
// in the string and evaluate the result.
eval(a.toString().replace(/"x"/, '"y"'));
// The new function modifies the y element.
a();
<div id="x">x</div>
<div id="y">y</div>