来自jQuery的Javascript中的等效函数
16lz
2021-01-22
$.ajax({
url: 'http://' + window.location.host + '/',
success: function(data){
$(data).find("a:contains(.jpg)").each(function(){
// will loop through
var images = $(this).attr("href");
$('<p></p>').html(images).appendTo('a div of your choice')
});
}
});
I couldn't find a way to do the same in javascript, I can make ajax call like this
我找不到在javascript中执行相同操作的方法,我可以像这样进行ajax调用
request = new XMLHttpRequest();
request.open('GET', 'http://' + window.location.host + '/', true);
request.onload = function(files) {
if (request.status >= 200 && request.status < 400){
// Success!
resp = request.responseText;
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
but how do I get the list of the files in the directory?
但是如何获取目录中的文件列表?
CSJS and/or SSJS both answers are okay. My main goal is not to use jQuery to accomplish what I want.
CSJS和/或SSJS两个答案都没问题。我的主要目标是不使用jQuery来实现我想要的。
2 个解决方案
#1
2
If you want to loop through the a:contains(.jpg)
like in your jQuery example, your best bet is probably to use a DocumentFragment and then call .querySelectorAll
on it :
如果你想在你的jQuery示例中循环遍历a:contains(.jpg),你最好的选择可能是使用DocumentFragment然后在其上调用.querySelectorAll:
var div = document.createElement('div');
div.innerHTML = request.responseText;
// if you want to search using text
var links = div.querySelectorAll('a')
for (i = 0; i < links.length; i++) {
var link = links[i];
if (!~link.innerHTML.indexOf('.jpg'))
continue;
// found one !
}
// if you want to search using an attribute
var links = div.querySelectorAll("a[href*='.jpg']");
更多相关文章
- 高德地图api接口poi检索示例----并在信息框显示经纬度
- Javascript学习:案例7--对象属性和方法的遍历、删除、添加.html
- 利用javascript实现遍历xml文件的代码实例
- Objective-C方法/函数调用。 (来自javascript示例)
- 有一个简单但有用的jquery.JsPlumb示例吗?
- js中ajax获取json数据遍历提示undefined
- python (9)统计文件夹下的所有文件夹数目、统计文件夹下所有文件数
- $.each遍历JSON字符串和 Uncaught TypeError: Cannot use 'in' o
- 获取错误“ValueError:int()的无效文字,基数为10:'3128;'在运行Tensor