Javascript 如何将DOM的值和属性放入文本框中?
我不知道如何在这段代码中拆分变量,因为它是一个字符串,所以我可以在文本框输入中键入:document.body.style.backgroundColor=“red”,或者简单地键入color=“red”。我是否需要在某一点使用正则表达式作为引号,或者如果不可能的话,即使没有引号,我怎么做呢Javascript 如何将DOM的值和属性放入文本框中?,javascript,string,input,Javascript,String,Input,我不知道如何在这段代码中拆分变量,因为它是一个字符串,所以我可以在文本框输入中键入:document.body.style.backgroundColor=“red”,或者简单地键入color=“red”。我是否需要在某一点使用正则表达式作为引号,或者如果不可能的话,即使没有引号,我怎么做呢 <html> <body> <script> var textbox = document.createElement("INPUT"); textbo
<html>
<body>
<script>
var textbox = document.createElement("INPUT");
textbox.setAttribute("type", "text");
textbox.setAttribute("value", code);
document.body.appendChild(textbox);
var color;
var code;
setInterval(function () {
var color = y+textbox.value;
code = textbox.value; // the code does not process in the text box at all
y=code.split("=");
document.body.style.[y]=color;
console.log(color);
}, 100);
</script>
</body>
</html>
var textbox=document.createElement(“输入”);
setAttribute(“类型”、“文本”);
textbox.setAttribute(“值”,代码);
document.body.appendChild(文本框);
颜色变异;
var代码;
setInterval(函数(){
var color=y+textbox.value;
code=textbox.value;//代码根本不在文本框中处理
y=代码拆分(“=”);
document.body.style.[y]=颜色;
控制台。日志(颜色);
}, 100);
好吧,就像我理解的那样,您希望文本框中的文本作为代码运行。您应该使用eval()函数,该函数接受字符串并将其作为代码进行处理/运行,但据我所知,它是非常不安全的(代码注入攻击),如果您确实不知道它会给您的网站/服务器带来什么风险,建议您完全不要使用它。
例如:
var string = "alert('hello');";
eval(string);
好吧,就像我理解的那样,你希望文本框中的文本作为代码运行。您应该使用eval()函数,该函数接受字符串并将其作为代码进行处理/运行,但据我所知,它是非常不安全的(代码注入攻击),如果您确实不知道它会给您的网站/服务器带来什么风险,建议您完全不要使用它。 例如:
var string = "alert('hello');";
eval(string);
好吧,就像我理解的那样,你希望文本框中的文本作为代码运行。您应该使用eval()函数,该函数接受字符串并将其作为代码进行处理/运行,但据我所知,它是非常不安全的(代码注入攻击),如果您确实不知道它会给您的网站/服务器带来什么风险,建议您完全不要使用它。 例如:
var string = "alert('hello');";
eval(string);
好吧,就像我理解的那样,你希望文本框中的文本作为代码运行。您应该使用eval()函数,该函数接受字符串并将其作为代码进行处理/运行,但据我所知,它是非常不安全的(代码注入攻击),如果您确实不知道它会给您的网站/服务器带来什么风险,建议您完全不要使用它。 例如:
var string = "alert('hello');";
eval(string);
我认为您的问题在于document.body.style.[y]=color行。您似乎正在将拆分的字符串包装到数组中,然后尝试将其用作标识符。我认为您的问题在于行
document.body.style.[y]=color
。您似乎正在将拆分的字符串包装到数组中,然后尝试将其用作标识符。我认为您的问题在于行document.body.style.[y]=color
。您似乎正在将拆分的字符串包装到数组中,然后尝试将其用作标识符。我认为您的问题在于行document.body.style.[y]=color
。您似乎正在将拆分的字符串包装到数组中,然后尝试将其用作标识符。1.创建一个
。并给它一个ID以访问它
function createInput() {
var input = document.createElement("input");
input.setAttribute("type", "text");
input.id = "myInput";
input.value = "color=red"; // or "document.body.style.backgroundColor=red"
document.body.appendChild(input);
}
2.设置
的背景色
注:
var str = "color=red";
var res = str.split("=");
//res is an array "["color", "red"]
//the second value in the array is res[1], which is the color
设置间隔的用法:
setInterval(function() {
f();
}, interval);
1.创建一个
。并给它一个ID以访问它
function createInput() {
var input = document.createElement("input");
input.setAttribute("type", "text");
input.id = "myInput";
input.value = "color=red"; // or "document.body.style.backgroundColor=red"
document.body.appendChild(input);
}
2.设置
的背景色
注:
var str = "color=red";
var res = str.split("=");
//res is an array "["color", "red"]
//the second value in the array is res[1], which is the color
设置间隔的用法:
setInterval(function() {
f();
}, interval);
1.创建一个
。并给它一个ID以访问它
function createInput() {
var input = document.createElement("input");
input.setAttribute("type", "text");
input.id = "myInput";
input.value = "color=red"; // or "document.body.style.backgroundColor=red"
document.body.appendChild(input);
}
2.设置
的背景色
注:
var str = "color=red";
var res = str.split("=");
//res is an array "["color", "red"]
//the second value in the array is res[1], which is the color
设置间隔的用法:
setInterval(function() {
f();
}, interval);
1.创建一个
。并给它一个ID以访问它
function createInput() {
var input = document.createElement("input");
input.setAttribute("type", "text");
input.id = "myInput";
input.value = "color=red"; // or "document.body.style.backgroundColor=red"
document.body.appendChild(input);
}
2.设置
的背景色
注:
var str = "color=red";
var res = str.split("=");
//res is an array "["color", "red"]
//the second value in the array is res[1], which is the color
设置间隔的用法:
setInterval(function() {
f();
}, interval);
什么是正确的方法,你能具体点吗?什么是正确的方法,你能具体点吗?什么是正确的方法,你能具体点吗?什么是正确的方法,你能具体点吗?你想做什么?在文本框中输入代码拆分(=)颜色,其中代码是document.body.style.backgroundColor。在创建/设置变量之前,您正在使用
code
执行setAttribute。您想做什么?在文本框中键入代码拆分(“=”)颜色,其中代码是document.body.style.backgroundColor。在创建/设置变量之前,您正在使用code
执行setAttribute。您想做什么?在文本框中键入代码拆分(“=”)颜色,其中代码是document.body.style.backgroundColor。在创建/设置变量之前,您正在使用code
执行setAttribute。您想做什么?在文本框中键入代码拆分(“=”)颜色,其中代码是document.body.style.backgroundColor。在创建/设置变量之前,您正在使用code
执行setAttribute。感谢这条建议,因为我之前考虑过。感谢这条建议,因为我之前考虑过。感谢这条建议,因为我早就想到了。谢谢你的建议,因为我早就想到了。