Javascript 使用任意变量生成函数

Javascript 使用任意变量生成函数,javascript,jquery,html,Javascript,Jquery,Html,我对javascript非常陌生,在使用这个场景时遇到了一些麻烦:我生成了元素#someotheridx,其中x是第一个元素的1,并且随着每个添加的元素而增长,所以#someotherid1#someotherid2#someotherid3等等 我想将其替换为输入字段#someidx。通过使用下面的代码(称为onclick of a button),我可以让它为任何元素工作,但我需要制作100个这样的元素(将1替换为2等等),因此如果用户创建了100个元素,它仍然可以工作 如何使一个函数适用于

我对javascript非常陌生,在使用这个场景时遇到了一些麻烦:我生成了元素
#someotheridx
,其中x是第一个元素的1,并且随着每个添加的元素而增长,所以
#someotherid1
#someotherid2#someotherid3等等

我想将其替换为输入字段
#someidx
。通过使用下面的代码(称为onclick of a button),我可以让它为任何元素工作,但我需要制作100个这样的元素(将1替换为2等等),因此如果用户创建了100个元素,它仍然可以工作

如何使一个函数适用于所有元素?使用while还是if

function aaa1()
{
    var input = $('<input>', { val: $("#someid1").text(), class:"someclass",
                              type: "text" });
    $("#someotherid1").replaceWith(input);
    input.select();  
}

function aaa2()
{
    var input = $('<input>', { val: $("#someid2").text(), class:"someclass",
                              type: "text" });
    $("#someotherid2").replaceWith(input);
    input.select();  
}
函数aaa1()
{
var input=$('',{val:$(“#someid1”).text(),类:“someclass”,
输入:“文本”};
$(“#someotherid1”)。替换为(输入);
input.select();
}
函数aaa2()
{
var input=$('',{val:$(“#someid2”).text(),类:“someclass”,
输入:“文本”};
$(“#someotherid2”)。替换为(输入);
input.select();
}

只有一个功能可以让您的场景正常工作

使用元素的索引/编号调用以下函数,它将为您带来奇迹:)

函数公共函数(索引)
{
var input=$('',{val:$(“#someid”+index).text(),类:“someclass”,
输入:“文本”};
$(“#someotherid”+索引)。替换为(输入);
input.select();
}

谢谢,再简单不过了。:)
function CommonFunction(index)
{
    var input = $('<input>', { val: $("#someid"+index).text(), class:"someclass",
                              type: "text" });
    $("#someotherid"+index).replaceWith(input);
   input.select();  
}