Javascript 页面加载时如何更改表单输入的背景色?
(我知道我可以用输入样式直接完成,但我有其他意图) 我试过几种方法Javascript 页面加载时如何更改表单输入的背景色?,javascript,forms,input,Javascript,Forms,Input,(我知道我可以用输入样式直接完成,但我有其他意图) 我试过几种方法 <script> document.getElementById('myTextBox').style.backgroundColor = 'red'; </script> <script> document.form1.myTextBox.style.backgroundColor = 'red'; </script> document.getElementById('myTe
<script> document.getElementById('myTextBox').style.backgroundColor = 'red'; </script>
<script> document.form1.myTextBox.style.backgroundColor = 'red'; </script>
document.getElementById('myTextBox').style.backgroundColor='red';
document.form1.myTextBox.style.backgroundColor='red';
我试着把它戴在头上,戴在身上。我不知道还能做什么使用onload
<body onload="style()">
把你的代码放在函数里,在正文的底部
<script type="text/javascript">
function style(){
document.getElementById('myTextBox').style.backgroundColor = 'red';
}
</script>
</body>
函数样式(){
document.getElementById('myTextBox').style.backgroundColor='red';
}
您需要告诉浏览器在您的案例中何时执行该语句。
将其作为函数调用放入body的onload就可以了。
看看这个例子
<html>
<head>
<script>
function FooBarFunction()
{
document.getElementById('foo').style.backgroundColor = 'red';
}
</script>
</head>
<body onload="FooBarFunction()">
<input type="textbox" id="foo">
</body>
</html>
函数FooBarFunction()
{
document.getElementById('foo').style.backgroundColor='red';
}
试试这个
$(function(){
$("#myTextBox").css("background","red");
});
示例:您可以将代码放在body的标记中
<body >
<script> document.getElementById('myTextBox').style.backgroundColor = 'red'; </script>
document.getElementById('myTextBox').style.backgroundColor='red';
只需将脚本放在前面即可 e、 g
document.form1.myTextBox.style.backgroundColor='red';
或者像这样编辑脚本
<script>window.onload =function(){ document.getElementById('myTextBox').style.backgroundColor = 'red';
}
</script>
window.onload=function(){document.getElementById('myTextBox')。style.backgroundColor='red';
}
只需将脚本放在正文的末尾就可以修复它。我最初把它放在身体的开头。谢谢令人惊叹的。如果它对您有帮助,请投票并标记为最佳答案。Document ready在DOM准备就绪后工作,如果有许多图像或脚本要加载,则此操作将失败$load(function(){/*stuff*/})更合适。
<script>window.onload =function(){ document.getElementById('myTextBox').style.backgroundColor = 'red';
}
</script>