gpt4 book ai didi

javascript - 使用 onClick 禁用文本字段时更改文本颜色?

转载 作者:行者123 更新时间:2023-11-28 01:58:44 25 4
gpt4 key购买 nike

这里我想做的是当我们点击按钮时,文本字段的默认值颜色应该被改变。我的代码适用于除 IE 之外的所有浏览器。如何在 IE 中克服这个问题。如何为下拉列表做。

<html>
<head>
<title> Change color for text when it is Disabled </title>
<meta name="Generator" content="EditPlus">
<script type="text/javascript">
<!--
/*function changeColor(){
document.getElementById('button_1').style.color='red';
}*/
function changeColor(){
document.getElementById('color').removeAttribute('disabled');
document.getElementById('color').style.color='red';
document.getElementById('color').setAttribute('readOnly','readOnly');
}
//-->
</script>
</head>

<body>
<input type="text" value='Hi Conrep' disabled id='color'><br />
<select id='color' disabled>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
</select><br />
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type='button' value="Change Color" onclick ='changeColor();'>
</body>
</html>

最佳答案

对于跨浏览器,您需要使用 readonly 属性而不是禁用。

示例

<style type="text/css">
.redColor
{
color:red;
}
</style>
<script type="text/javascript">
<!--
function changeColor()
{
var el = document.getElementById('button_1');
el.className = "redColor";
}
//-->
</script>
<input type="text" value='Hi Conrep' readonly id='button_1'>
<input type='button' value="Change Color" onClick ='changeColor();'>

关于javascript - 使用 onClick 禁用文本字段时更改文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15355811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com