javascript,ajax获取本地文件JSON

时间:2014-09-18 22:40:49

标签: javascript ajax json

我正在尝试让我的代码加载我的 index.html script.js 样式旁边的文件夹中的JSON文件。 CSS 。文件夹名称为文章,文件名为 article article1 article2 article3 article4 article5

有人能看出为什么它不起作用吗?

HTML:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div id='meny'>
      <button>No article</button>
      <button>article1</button>
      <button>article2</button>
      <button>article3</button>
      <button>article4</button>
      <button>article5</button>
    </div>
    <div id='div2'>
      <p id='selectedArticle'>No article selected</p>
      <div>
        Författare:<a href="" id='author'></a>
      </div>
      <div>
        Datum:<span id='date'></span>
      </div>
      <p id='text'>select article</p>
    </div>
    <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
    <script src="script.js"></script>
    <meta charset="UTF-8">
  </body>
</html>

的script.js:

$(document).ready(function() {

  var buttons = $("button");
  var setFirstButtonBold = function() {
    buttons.first().css("font-weight","bold");
  };

  setFirstButtonBold();

  $('#meny button').on('click',function () {
    buttons.css("font-weight","normal");
    $(this).css("font-weight","bold");
    var buttonName = $(this).text();

    if($(this).text() == "No article") {
      $('#selectedArticle').html("No article selected");
      $('#author').attr("");
      $('#author').html("");
      $('#date').text("");
      $('#text').html("select article");
    } else {
      $('#text').text("Laddar....");
    }

    $.get('articles/'+buttonName,function(data) {
    $('#selectedArticle').html(data.title);
      $('#author').attr("href","mailto:"+data.author);
      $('#author').html(data.author);
      $('#date').text(data.date);
      $('#text').html(data.text);
    },'json');
  });
});

0 个答案:

没有答案