在pdf中使用javascript的多色更改按钮

时间:2013-06-02 23:43:22

标签: javascript button colors

我似乎无法进一步了解这一点。我更像是一个图形人而不是程序员 每次按下按钮时,我都需要一个按钮,从白色到灰色再到红色再到黑色再到白色。一个pdf文件中将有超过一百个这样的按钮 这是我到目前为止所尝试的:

if (this.getField("Button1").fillColor = ["RGB",1,1,1])
  {
  app.alert({ cMsg: "Switch to Grey", });
  this.getField("Button1").fillColor = ["RGB",.5,.5,.5];
  };

if (this.getField("Button1").fillColor = ["RGB",.5,.5,.5])
  {
  app.alert({ cMsg: "Switch to Red", });
  this.getField("Button1").fillColor = ["RGB",1,0,0];
  };

if (this.getField("Button1").fillColor = ["RGB",1,0,0])
  {
  app.alert({ cMsg: "Switch to Black", });
  this.getField("Button1").fillColor = ["RGB",0,0,0];
  };

if (this.getField("Button1").fillColor = ["RGB",0,0,0])
  {
  app.alert({ cMsg: "Switch to White", });
  this.getField("Button1").fillColor = ["RGB",1,1,1];
  };

这可能吗? 感谢

1 个答案:

答案 0 :(得分:0)

<强>更新

使用您自己的代码,它看起来像这样:

if (this.getField("Button1").fillColor == ["RGB",1,1,1])
 {
  app.alert({ cMsg: "Switch to Grey", });
  this.getField("Button1").fillColor = ["RGB",.5,.5,.5];
 }
else if (this.getField("Button1").fillColor == ["RGB",.5,.5,.5])
{
 app.alert({ cMsg: "Switch to Red", });
 this.getField("Button1").fillColor = ["RGB",1,0,0];
}
else if (this.getField("Button1").fillColor == ["RGB",1,0,0])
{
 app.alert({ cMsg: "Switch to Black", });
 this.getField("Button1").fillColor = ["RGB",0,0,0];
}
else if (this.getField("Button1").fillColor == ["RGB",0,0,0])
{
 app.alert({ cMsg: "Switch to White", });
 this.getField("Button1").fillColor = ["RGB",1,1,1];
}

我以前的答案 :(仅供参考)

下面的内容纯粹是针对JavaScript和HTML而不是PDF,但您将了解它是如何完成的。

您可以在JavaScript中使用getElementByIdrgb,如:

  document.getElementById("button1").style.backgroundColor == "rgb(0, 255, 0)"

我将在这里举一个例子。假设你有一个蓝色的按钮,如

<div>
<button id="button1" onclick="getcolor()" style="background-color:#0000FF; padding:5px;">Colored Button - Blue</button>
</div>

我们希望将其更改为绿色,如果是绿色我们想将其更改为蓝色,那么您将拥有这样的JavaScript:

function getcolor()
{
  var button_color = document.getElementById("button1").style.backgroundColor;
  if (button_color == "rgb(255, 255, 255)")
  {
    alert("Switch color to Grey");
    document.getElementById("button1").style.backgroundColor = "rgb(128, 128, 128)";
 }
 else if (button_color == "rgb(128, 128, 128)")
 {
    alert("Switch color to Red");
    document.getElementById("button1").style.backgroundColor = "rgb(255, 0, 0)";       
 }
 else if (button_color == "rgb(255, 0, 0)")
 {
     alert("Switch color to Black");
     document.getElementById("button1").style.backgroundColor = "rgb(0, 0, 0)";       
 }
 else if (button_color == "rgb(0, 0, 0)")
 {
     alert("Switch color to White");
     document.getElementById("button1").style.backgroundColor = "rgb(255, 255, 255)";       
 }
}

顺便说一下小心rgb中的空格比较例如蓝色时比较它不应该是rgb(0,0,255),它在颜色的每个十进制值之后没有空格,如:

 if (button_color == "rgb(0,0,255)")

但它应该是这样的:

 if (button_color == "rgb(0, 0, 255)")

查看我的Updated Demo