jQuery appendTo列表样式

jQuery appendTo list style

本文关键字:样式 列表 appendTo jQuery      更新时间:2023-09-26

我对jQuery/HTML5有些陌生,所以遇到了一些问题。希望你能帮忙。

我正在加载一个本地json文件,并希望将其中的项添加到列表中。这很好用。但是,该列表没有使用jQuery样式?

我在没有加载json文件的情况下进行了尝试,而是将数据直接放在代码中,结果就成功了。我可能有什么问题?

以下是样式不正确的代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>tuBS Energie App</title>
    <link href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.css" rel="stylesheet"/>
    <link href="http://code.jquery.com/mobile/1.3.0/jquery.mobile.structure-1.3.0.min.css" rel="stylesheet"/>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>   
</head>
<body>
    <div id="tasksPage" data-role="page">
        <div data-role="header" data-position="fixed">
            <a href="#" data-role="button">Zurück</a>
            <h1>tuBS Energie App</h1>
        </div>
        <div data-role="content">
            <h3>Institut für Nachrichtentechnik</h3>
            <ul id="taskList" data-role="listview" data-filter="true" data-filter-placeholder="Suche nach Institut..." data-inset="true"></ul>
        </div>
        <div data-role="footer" data-position="fixed">
            <a href="#" data-role="button">Info</a>
        </div>
    </div>
    <script type="text/javascript" charset="utf-8">
            $(function() 
            {
                $(document).ready(function(){
                    $.getJSON("institute.js",function(data) {
                       $.each(data.institute,function(i,el){
                           $("<li><a href='#'>"+el.id+". "+el.name+"</a> </li>").appendTo("#taskList");
                       });
                    });
                });
            })
    </script> 
</body>
</html>

这是给我正确样式的代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>tuBS Energie App</title>
    <link href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.css" rel="stylesheet"/>
    <link href="http://code.jquery.com/mobile/1.3.0/jquery.mobile.structure-1.3.0.min.css" rel="stylesheet"/>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>   
</head>
<body>
    <div id="tasksPage" data-role="page">
        <div data-role="header" data-position="fixed">
            <a href="#" data-role="button">Zurück</a>
            <h1>tuBS Energie App</h1>
        </div>
        <div data-role="content">
            <h3>Institut für Nachrichtentechnik</h3>
            <ul id="taskList" data-role="listview" data-filter="true" data-filter-placeholder="Suche nach Institut..." data-inset="true"></ul>
        </div>
        <div data-role="footer" data-position="fixed">
            <a href="#" data-role="button">Info</a>
        </div>
    </div>
    <script type="text/javascript" charset="utf-8">
            var institute_json = 
            [
               {"id":"1","name":"Adaptronik und Funktionsintegration"},
               {"id":"2","name":"Analysis und Algebra"},
               {"id":"3","name":"Angewandte Mechanik"},
               {"id":"4","name":"Angewandte Physik"},
               {"id":"5","name":"Anorganische und Analytische Chemie"},
               {"id":"6","name":"Architekturbezogene Kunst (IAK)"},
               {"id":"7","name":"Automobilwirtschaft u Industrielle Produktion"},
            ];
            $.each(institute_json,function(i,el)
            {
                $("<li><a href='#'>"+el.id+". "+el.name+"</a></li>").appendTo("#taskList");
            });
    </script> 
</body>
</html>

当您将元素动态添加到列表视图时,渲染后需要刷新它。

$("ul").listview("refresh");

你能给我们介绍一下institute.js 的内容吗

试试这个:

$.getJSON("institute.js",function(data) {
            $.each(data,function(i,el)