Javascript 单击数组中的每个ID
我需要能够遍历数组中的每个项目并单击它。数组表示需要单击的元素的ID。 下面是检索数组并将其放置在变量arrayResponse中的代码 //[“2”、“3”、“4”、“5”、“1”、“2”、“3”、“4”、“5”] HTMLJavascript 单击数组中的每个ID,javascript,php,Javascript,Php,我需要能够遍历数组中的每个项目并单击它。数组表示需要单击的元素的ID。 下面是检索数组并将其放置在变量arrayResponse中的代码 //[“2”、“3”、“4”、“5”、“1”、“2”、“3”、“4”、“5”] HTML 我假设图像的条目为ID arrayResponse.forEach(function(entry) { document.getElementById(entry).click(); //using js $("#"+entry).click(); //using jqu
我假设图像的条目为ID
arrayResponse.forEach(function(entry) {
document.getElementById(entry).click(); //using js
$("#"+entry).click(); //using jquery
});
您已经有了一个foreach循环来获取每个单独的ID,您需要单击每个ID,
以我为例:
function reqListener () {
console.log(this.responseText);
}
var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function() {
//The actual data is found on this.responseText
var arrayResponse = this.responseText; //Assign array to variable
alert(arrayResponse);
arrayResponse.forEach(function(entry) {
document.querySelector('#'+entry).click();
});
};
oReq.open("get", "load.php", true);
oReq.send();
HTML中是否有具有此ID的选择器?一个数组具有相同的多个元素,它们只是带有ID的图像,当用户单击图像时,会发生一些事情。我希望通过Javascript单击数组中的每个ID,以返回索引页上的结果“我需要”不是问题。你到底在问什么呢?如何添加一个Javascript函数来单击array@Luke检查我这里的纯JavaScript解决方案会更好,而这段代码可能会回答这个问题,提供关于这段代码为什么和/或如何回答这个问题的附加上下文可以提高其长期价值。一个好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的访客。
arrayResponse.forEach(function(entry) {
document.getElementById(entry).click(); //using js
$("#"+entry).click(); //using jquery
});
function reqListener () {
console.log(this.responseText);
}
var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function() {
//The actual data is found on this.responseText
var arrayResponse = this.responseText; //Assign array to variable
alert(arrayResponse);
arrayResponse.forEach(function(entry) {
document.querySelector('#'+entry).click();
});
};
oReq.open("get", "load.php", true);
oReq.send();