【问题标题】:jQuery click and load function do not workjQuery单击和加载功能不起作用
【发布时间】:2015-05-05 07:43:13
【问题描述】:

我正在尝试使用以下代码将文本从 txt 文档加载到 div:

$(document).ready(function(){
    $('button').click(function(){
        $('#contenthere').load('Load.txt');
    });
});

html:

<button> click me! </button>
<div id="contenthere">//Load.txt should load here!</div>

txt:

If this text is loaded then you've managed to succefully use jQuery!

点击功能正在工作,但加载功能根本不工作,我尝试了不同的方法来解决这个问题但没有成功,并且 Load.txt 文件与索引文件位于同一文件夹中是 jquery 加载代码已存储!

编辑: 这是我得到的错误:

XMLHttpRequest cannot load file:///C:/Users/nti/Google%20Drive/Gr%C3%A4nssnittsdesign/slutUppgift/exempel/Load.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

我也试过了:

$(document).ready(function(){
    $('button').click(function(){
        $.ajax({
            url: "Load.txt",
            dataType: "text",
            succes : function (data){
                $("#contenthere").html(data);
            }
        });
    });
});

得到了和上面一样的错误。

【问题讨论】:

  • @preyerguiltconsole 有什么错误
  • 您不能使用来自file:// 的AJAX(尽管这可能适用于某些旧浏览器)。你需要一个http:// 环境才能工作,这意味着从本地主机或你的服务器调用Load.txt。
  • 我已经测试了你的代码,它工作正常,请确保你有正确的权限来读取文件。另外,检查控制台输出的内容。
  • @ozil 我在问题编辑中写了错误

标签: javascript jquery click load


【解决方案1】:

我让代码工作,问题一定是我尝试使用 file:// 而不是 http:// 所以我将页面加载到 Webb 主机并让代码工作!

【讨论】:

    猜你喜欢
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多