交换文本颜色的功能必须单击两次才能工作



我知道为什么会这样。我只是不知道如何解决它。我正在从数据库字段中读取文本颜色。它将文本的颜色设置为数据库中的任何颜色(红色或绿色)。我有一个按钮可以通过函数调用来更改它。第一次按下按钮时,默认情况下会将文本更改为红色(即使它已经是红色)。我确定这是因为没有设置 style.color 值,但我不确定如何设置它,因为它必须是数据库中的任何内容。我该如何解决这个问题?下面是构建文件的 PHP 表单:

echo '<form name="form1" action="save_special_announcement.php" method="post">',
 '<h3><font color="',$row['txtColor'],'" id="currentColor">Current color of announcement&nbsp;</font><input type="button" onclick="colorRed('currentColor')" value="Change" ></h3>',
 '<input type=hidden id="txtColor" name="txtColor">',

我正在使用隐藏的 txtColor 发送到提交页面。这是函数:

function colorRed(input) { // Change text to red
if(document.getElementById(input).style.color != 'red') {   
       document.getElementById(input).style.color = 'red';
       document.getElementById('txtColor').value ='red'; }
else { document.getElementById(input).style.color = 'green';
       document.getElementById('txtColor').value ='green';}
};

我希望我尽可能地解释了这一点。谢谢你能给我的任何帮助。

  1. 请勿使用该标记。我会删除它并改用style="color:'.$row['txtColor'].'"'

  2. 因为你使用了font-tag而不是css,然后通过style.color更改了颜色,这改变了css,所以一切都混淆了。

相关内容

最新更新