【问题标题】:Multi-Color changing button using javascript in a pdf在pdf中使用javascript的多色更改按钮
【发布时间】:2013-06-02 23:43:22
【问题描述】:

我似乎无法进一步了解这一点。我更喜欢图形而不是程序员。
每次按下按钮时,我都需要一个按钮来从白色到灰色再到红色再到黑色再到白色。一个 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];
  };

这可能吗? 谢谢

【问题讨论】:

  • 你应该在这里改用if ... else if ... else。否则,我会看到每个 if 子句都在执行(因为它们会检查更改的值)。
  • 当您比较两个不单独使用 = 的值时。您使用 == 或 === 因为 = 意味着您将值分配给例如变量。
  • @user2446117 怎么了?您最终能找到解决问题的方法吗?

标签: javascript button colors


【解决方案1】:

更新:

使用您自己的代码会如下所示:

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

【讨论】:

  • 这在 pdf 文件中似乎不起作用...还有什么需要补充的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
  • 2018-12-02
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
相关资源
最近更新 更多