$.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']");

更多相关文章

  1. 高德地图api接口poi检索示例----并在信息框显示经纬度
  2. Javascript学习:案例7--对象属性和方法的遍历、删除、添加.html
  3. 利用javascript实现遍历xml文件的代码实例
  4. Objective-C方法/函数调用。 (来自javascript示例)
  5. 有一个简单但有用的jquery.JsPlumb示例吗?
  6. js中ajax获取json数据遍历提示undefined
  7. python (9)统计文件夹下的所有文件夹数目、统计文件夹下所有文件数
  8. $.each遍历JSON字符串和 Uncaught TypeError: Cannot use 'in' o
  9. 获取错误“ValueError:int()的无效文字,基数为10:'3128;'在运行Tensor

随机推荐

  1. (转载)再谈Android AsyncTask
  2. Android Studio在Gradle中隐藏Keystore密
  3. Ubuntu10.04 32位编译Android 4.0源码
  4. Android定时器的使用,
  5. Android手势滑动(左滑和右滑)
  6. 用网络adb连接调试Android
  7. Android常见问题总结(七)
  8. Android BitmapShader 实战 实现圆形、圆
  9. android binder机制之--(我是Server Porxy
  10. IOS与Android的session的存储区别