【问题标题】:Jquery get url for "href", from an external fileJquery 从外部文件获取“href”的 url
【发布时间】:2014-03-24 13:04:53
【问题描述】:

我想从外部文件加载一个 url,用于属性“href”。我的代码:

var url = $("file.php div#data").html()

$("a#myLink").attr('href',url);

这不起作用。为什么 ?我应该使用“.load”吗? “.ajax”?谢谢。

【问题讨论】:

    标签: jquery load href external attr


    【解决方案1】:

    您可以使用多个 AJAX 函数来实现,例如:

    $.ajax('file.php').done(function(e) {
        $('a#myLink').attr('href', e);
    });
    

    【讨论】:

      【解决方案2】:

      您需要使用 AJAX 来加载文件。但是,使用 PHP 输出链接的 JS 可能更容易,因为没有 AJAX 的 JavaScript 无法访问文件(由于 JS 引擎的安全设置)。

      var txtFile = new XMLHttpRequest(); txtFile.open("GET", "http://my.remote.url/myremotefile.txt", true); txtFile.onreadystatechange = function() { if (txtFile.readyState === 4) { // Makes sure the document is ready to parse. if (txtFile.status === 200) { // Makes sure it's found the file. allText = txtFile.responseText; lines = txtFile.responseText.split("\n"); // Will separate each line into an array } } } txtFile.send(null);

      答案转载自https://stackoverflow.com/a/5437603/2225787

      【讨论】:

      • 如果它适合您,请通过单击勾选将此 aor iCore 的答案标记为已接受。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多