简体   繁体   English

找到文件后如何显示隐藏的div?

[英]How to make hidden div which appears when file is found?

I was trying to make hidden div which shows on when .txt file is found. 我正在尝试使隐藏的div在找到.txt文件时显示。 I want the div to show the content of the .txt file. 我希望div显示.txt文件的内容。

Is that possible? 那可能吗?

I have tried to make it by using jQuery but without success. 我试图通过使用jQuery来实现,但是没有成功。

Idea is simple: div box with constant dimensions which shows content of .txt file which is in same folder on server, but when there isn't any .txt file div becomes hidden (for example in jQuery $("p").hide(); ). 想法很简单:具有恒定尺寸的div框,用于显示.txt文件的内容,该文件位于服务器上的同一文件夹中,但是当没有任何.txt文件时,div就会被隐藏(例如在jQuery $("p").hide(); )。

As far as I know this is not possible. 据我所知这是不可能的。 You can do an ajax call to a php-page which will check if the file exist on the server and returns something, but directly loading/checking using jQuery is in violation of browser security so they should not allow you to do that. 您可以对php页进行ajax调用,以检查服务器上是否存在该文件并返回某些内容,但是使用jQuery直接加载/检查违反了浏览器的安全性,因此它们不应允许您这样做。

This depends on the usage, but say you have your div for the text, you can make that invisible by setting it to display:none in css. 这取决于用法,但是说您有文本的div,则可以通过在CSS中将其设置为display:none使其不可见。 Now you can either try getting the file with an ajax call, or you can use a ajax head call to check if the file exists on the server. 现在,您可以尝试通过ajax调用获取文件,也可以使用ajax头调用来检查服务器上是否存在该文件。 Just getting the file is faster if it exists, as a head call only checks if it's there, and another ajax call would be neccessary to get the content of the file, however a head call is faster it it does not exist as it does'nt get the content of the file. 如果存在,只是获取文件会更快,因为head调用只会检查文件是否存在,并且需要另一个ajax调用来获取文件的内容,但是head调用会更快,因为它不存在,因为它确实存在。 nt获取文件的内容。

EDIT: but since the file does not exists, there is nothing to get, so a head call is probably not needed here as a "get" would be faster in both situations, but the example still shows how to check if a file exists on the server, and then do something if it does or does not exist. 编辑:但是由于文件不存在,所以没有什么要获取的,因此这里可能不需要head调用,因为“ get”在两种情况下都更快,但是该示例仍然显示了如何检查文件是否存在服务器,然后执行某些操作(如果存在或不存在)。

It would look like something like this. 看起来像这样。

$.ajax({
    url:'http://www.mysite.com/myfile.txt',
    type:'HEAD',
    error: function()
    {
        //file does not exists, no need to do anything
    },
    success: function()
    {
        //file exists get the file and put in the textcontainer and make that visible
        $.ajax({
           url:'http://www.mysite.com/myfile.txt',
           success: function(data)
              {
                 $("#textcontainer").show().text(data);
              }

    }
 });

Or the other way around with the textcontainer visible with display:block etc : 或者以另一种方式使textcontainer与display:block等可见:

$.ajax({
    url:'http://www.mysite.com/myfile.txt',
    type:'GET',
    error: function()
    {
        //file does not exists, hide the textcontainer
        $("#textcontainer").hide();
    },
    success: function(data)
    {
        //file exists get the file and put in the textcontainer that is already visible
         $("#textcontainer").text(data);
    }
 });

According to http://api.jquery.com/jQuery.get/ you can handle errors: 根据http://api.jquery.com/jQuery.get/,您可以处理错误:

If a request with jQuery.get() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method. 如果使用jQuery.get()的请求返回错误代码,除非脚本还调用了全局.ajaxError()方法,否则该请求将静默失败。

Please also see http://api.jquery.com/jQuery.ajax/#jqXHR 另请参阅http://api.jquery.com/jQuery.ajax/#jqXHR

An example from that page: 该页面上的示例:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax({ url: "example.php" })
    .success(function() { alert("success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM