如何使用jQuery基于JSON文件动态更改数据

时间:2016-02-12 04:00:48

标签: javascript jquery json

<div id="rightside">
            <h1>Name Of Person</h1>
            <p>1900-1950
                <p><a href="linktowiki">Learn More</a>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur porta dictum turpis, eu mollis justo gravida ac. Proin non eros blandit, rutrum est a, cursus quam. Nam ultricies, velit ac suscipit vehicula, turpis eros sollicitudin lacus, at
                        convallis mauris magna non justo. Etiam et suscipit elit. Morbi eu ornare nulla, sit amet ornare est. Sed vehicula ipsum a mattis dapibus. Etiam volutpat vel enim at auctor.</p>
                    <p>Aenean pharetra convallis pellentesque. Vestibulum et metus lectus. Nunc consectetur, ipsum in viverra eleifend, erat erat ultricies felis, at ultricies mi massa eu ligula. Suspendisse in justo dapibus metus sollicitudin ultrices id sed nisl.</p>
                    <button>Pause</button>
        </div>

我的div看起来像这样。我想用我从JSON文件获取的数据替换它。但是我只希望一次显示1个条目并使其在条目之间生成动画(不需要花哨)。

我的问题是如何让jQuery在我的JSON数据之间进行更改?

我的JSON数据看起来像

[
  {
    "name": "Elizabeth Cady Stanton",
    "image": "media/images/elizabeth.jpg",
    "year": "1815 – 1902",
    "wikilink": "https://en.wikipedia.org/wiki/Elizabeth_Cady_Stanton",
    "article": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur porta dictum turpis, eu mollis justo gravida ac. Proin non eros blandit, rutrum est a, cursus quam. Nam ultricies, velit ac suscipit vehicula, turpis eros sollicitudin lacus, at convallis mauris magna non justo. Etiam et suscipit elit. Morbi eu ornare nulla, sit amet ornare est. Sed vehicula ipsum a mattis dapibus. Etiam volutpat vel enim at auctor. Aenean pharetra convallis pellentesque. Vestibulum et metus lectus. Nunc consectetur, ipsum in viverra eleifend, erat erat ultricies felis, at ultricies mi massa eu ligula. Suspendisse in justo dapibus metus sollicitudin ultrices id sed nil."
  },

我希望得到任何帮助或链接到示例。

由于

3 个答案:

答案 0 :(得分:1)

添加了data-index来跟踪我们正在展示的数据索引

<div id="rightside" data-index='-1'>..</div>

它将以旋转方式每5秒更改一次数据。

setTimeout(function(){
        var yourJsonData = [{"name":"Elizabeth Cady Stanton","image":""media/images/elizabeth.jpg"},{"name":"Elizabeth Cady Stanton","image":""media/images/elizabeth.jpg"}];
        var currentIndex = $("#rightside").attr('data-index');
        var totalLengthOfData = yourJsonData.length;
        var indexToShow = currentIndex+1;
        if(indexToShow > totalLengthOfData){
           indexToShow=0;
        }
        $("#rightside").attr('data-index',indexToShow);
        var dataToAppend =  yourJsonData[indexToShow];

        $("#rightside").children().remove();
        $("#rightside").append("<h1>"+yourJsonData['name']+"</h1>").
                        append("<p>"+yourJsonData['year']+"</p><p><a href='"+yourJsonData['linktowiki']+"'>learn more</a></p>"); 

     },5000);

尚未测试代码

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>


<script>
    $(document).ready(function () {
        var yourJsonData = '[{"name": "Elizabeth Cady Stanton","image": "media/images/elizabeth.jpg","year": "1815 – 1902","wikilink": "https://en.wikipedia.org/wiki/Elizabeth_Cady_Stanton",            "article": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur porta dictum turpis, eu mollis justo gravida ac. Proin non eros blandit, rutrum est a, cursus quam. Nam ultricies, velit ac suscipit vehicula, turpis eros sollicitudin lacus, at convallis mauris magna non justo. Etiam et suscipit elit. Morbi eu ornare nulla, sit amet ornare est. Sed vehicula ipsum a mattis dapibus. Etiam volutpat vel enim at auctor. Aenean pharetra convallis pellentesque. Vestibulum et metus lectus. Nunc consectetur, ipsum in viverra eleifend, erat erat ultricies felis, at ultricies mi massa eu ligula. Suspendisse in justo dapibus metus sollicitudin ultrices id sed nil."}]'
        var obj = JSON.parse(yourJsonData);
        $('#rightside').find('h1:first').text(obj[0].name);
        $('#rightside').find('p:first').text(obj[0].year);
        $('#rightside').find('a:first').attr('href', obj[0].wikilink)
        $('#rightside').find('p:eq(3)').text(obj[0].article)
    });


</script>



<body>
  <div id="rightside">
            <h1>Name Of Person</h1>
            <p>1900-1950
                <p><a href="linktowiki">Learn More</a>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur porta dictum turpis, eu mollis justo gravida ac. Proin non eros blandit, rutrum est a, cursus quam. Nam ultricies, velit ac suscipit vehicula, turpis eros sollicitudin lacus, at
                        convallis mauris magna non justo. Etiam et suscipit elit. Morbi eu ornare nulla, sit amet ornare est. Sed vehicula ipsum a mattis dapibus. Etiam volutpat vel enim at auctor.</p>
                    <p>Aenean pharetra convallis pellentesque. Vestibulum et metus lectus. Nunc consectetur, ipsum in viverra eleifend, erat erat ultricies felis, at ultricies mi massa eu ligula. Suspendisse in justo dapibus metus sollicitudin ultrices id sed nisl.</p>
                    <button>Pause</button>
</div>
 </body>
</html>

答案 2 :(得分:0)

你的问题可以解决:

HTML:

<div id="rightside">
<h1 id="nameOfPerson"></h1>
 <p id="year">
 </p>
<a id="wikiLink">Learn More</a>
<p id="article"></p>
<p></p>
<button id="fill">Start</button>
</div>

JQuery:

var dataObject = [{
"name": "Elizabeth Cady Stanton",
"image": "media/images/elizabeth.jpg",
"year": "1815 – 1902",
"wikilink": "https://en.wikipedia.org/wiki/Elizabeth_Cady_Stanton",
"article": "Whole Article"
}];

$("#fill").click(function() {
setData(dataObject);
});

var setData = function(data) {
$.each(data, function(index, mainData) {
$("#nameOfPerson").text(mainData.name);    
$("#year").text(mainData.year);
$("#wikiLink").attr("href",mainData.wikilink);
$("#article").text(mainData.article)
});

};

通过此代码,您的数据将动态呈现。 对于演示,你可以检查我的小提琴: Fiddle Link Here