[英]“Document not defined” Javascript with IMacros
我正在尝试在网页上运行用javascript编写的IMacros宏,如下所示:
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;
}
}
从js控制台看,一切似乎都运行良好,但是当我运行宏时,我得到:
"ReferenceError: document is not defined, line 2 (Error code: -991)"
我用这个将JQuery加载到iMacros中,并将我的代码放在:
$(document).ready(function () {
//
});
但是如果我使用JQuery,我会不断收到此错误:
TypeError: $ is not a function, line 28 (Error code: -991)
如果我只使用JS,我会像以前一样得到“文件未定义”错误。
所以我的问题是,我是否需要定义文档,我该怎么做?
我从来没有能够将jQuery加载到imacros脚本中,但这最终并不是什么大问题。 要访问DOM,您需要将每个元素称为: window.content.document.getElementsByClassName('foo')
。 这将为您提供一个数组,因此请务必选择所需数组中的每个元素:
var foo_class = window.content.document.getElementsByClassName('foo');
for (i=0;i<foo_class.length;i++){
//do something
}
希望能帮助到你
编辑添加工作示例:
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))
复制宏.js中的代码并在stackexchage上的firefox中运行它。 它将使用class =“question-hyperlink”计算所有链接并显示其各自的文本 - 您可以在Play(循环)按钮下的绿色文本框中看到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.