当前位置:首页 > 操作系统 > MacOs

“文档未定义”与IMacros的Javascript

我正在尝试在网页上运行用javascript编写的IMacros宏,如下所示:

<code>for (var i = 1; i < 18; i++) {
   document.querySelector(".foo table > tbody > tr:nth-child(" + i + ") > .goo:nth-child(2) > a").click();
   document.querySelector(".foo > a").click();

if (i % 17===0) {
    alert('Reset i');
    i = 1;
    }
}
</code>

从js控制台看,一切似乎都运行良好,但是当我运行宏时,我得到:

<code>"ReferenceError: document is not defined, line 2 (Error code: -991)"
</code>

我用this将JQuery加载到iMacros中,并将我的代码放在:

<code>$(document).ready(function () {
    //
}); 
</code>

但是如果我使用JQuery,我会不断收到此错误:

<code>TypeError: $is not a function, line 28 (Error code: -991)
</code>

如果我只使用JS,我会像以前一样得到“文件未定义”错误.

所以我的问题是,我是否需要定义文档,我该怎么做?

解决方法:

我从来没有能够将jQuery加载到imacros脚本中,但这最终并不是什么大问题.
要访问DOM,您需要将每个元素引用为:
例如,window.content.document.getElementsByClassName(‘foo’).
这将为您提供一个数组,因此请务必选择所需数组中的每个元素:

<code>var foo_class = window.content.document.getElementsByClassName('foo');

for (i=0;i<foo_class.length;i++){
//do something
}
</code>

希望能帮助到你

编辑添加工作示例:

<code>var links = window.content.document.getElementsByClassName('question-hyperlink');
var list=[]
for (i=0;i<links.length;i++){ 
txt=links[i].innerHTML;
list.push(txt);
}
number=links.length;
linkstexts=list.toString();
showme="number of links with class=question-hyperlink: "+number+"       text links with class=question-hyperlink: "+linkstexts;

iimDisplay((showme))
</code>

复制宏.js中的代码并在stackexchage上的firefox中运行它.它将使用class =“question-hyperlink”计算所有链接并显示其各自的文本 – 您可以在Play(循环)按钮下的绿色文本框中看到它.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐