尝试在JavaScript中用红色突出显示一个单词

问题描述

我有一个Adobe PDF文档,该文档是我在创建从一页到另一页的链接的地方创建的。在下面的JavaScript代码中,我正在搜索“ 120”(word ==“ 120”)。我想要发生的是……当最终用户单击其初始链接并转到显示该文本的页面时,我希望文本以“红色”突出显示,以便在图纸上进行标注在许多其他标注编号中找到我不想将文本更改为红色...我想突出显示它,但我不知道该怎么做。

对于其中我在pdf页面中具有多个位置且具有与我要查找的单词相同的位置的实例,插入if (nWait == "0")语句。

这是我一直在使用的代码,这与他们的“运行JavaScript操作”一起插入到我的Adobe pdf中。我完全迷路了,有人可以帮我吗?

nWait = 0;
nWords = getPageNumWords(85);
for (var loop = 0; loop < [nWords - 1]; loop++) {
  word = getPageNthWord(85,loop);
  if (word == "1") {
    if (nWait == "0") {
      this.selectPageNthWord(85,loop);
      break;
    }
    nWait = nWait + 1;
  }
}

解决方法

我无法在Acrobat中运行它,但这应该使您更接近目标。如果我了解您要完成的工作,那么您只想选择一个字。为此,您的nWait变量不是必需的,只需在找到所需内容后使用break即可退出循环。


// Get the number of words on the page
var nWords = getPageNumWords(85);

// Loop over all of them. Your end-condition of the loop should just be `nWords` in this case. If the number of words is 5,your loop is going to run from 0 to 4,which is 5 times (which is what you want).
for (var loop = 0; loop < nWords; loop++) {

  // Get the word from Acrobat
  var word = getPageNthWord(85,loop);

  // If this word is what we're looking for...
  if (word === "1") {

    // Select it on the page,and then break the loop as we're done
    this.selectPageNthWord(85,loop);
    break;
  }
}