如何使用javascript对象作为li元素的id属性或锚标记id

时间:2015-02-17 11:28:52

标签: javascript jquery json

<script type="text/javascript">

        function getNews() {

            var xmlhttp = new XMLHttpRequest();
            var url = "http://192.168.2.100:7172/SchoolyServiceModel1004/rest/newsservice/news";
            xmlhttp.open("GET", url, true);
            xmlhttp.setRequestHeader("Content-type", "application/json");
            xmlhttp.send();

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {

                    if (xmlhttp.status == 200) {
                        var news = JSON.parse(xmlhttp.responseText);
                        showNews(news);
                    }

                }
            }
        };



        function showNews(news) {

            var i, ntitle, ncontent, ndate, nid;

            for (i = 0; i < news.length; i++) {
                ndate = news[i].PublishDate;
                **ntitle = cur[i].title;**      /// this object i want to use as 
                                                     id attribute of anchor tag 
                                                     or li 
                nid = news[i].id;
                ncontent = news[i].content;


                var cont = document.getElementById("cont");
                var div1 = document.getElementById("div1");
                var panelHead = document.getElementById("Heading");
                var NewsHead = document.getElementById("NewsHead");
                var head = document.getElementById("head");
                div1.setAttribute("class", " col-md-3 panel panel-primary");
                panelHead.setAttribute("class", "panel-heading");
                NewsHead.setAttribute("class", "panel-title");
                head.appendChild(document.createTextNode("News Title"));
                panelHead.appendChild(head);
                panelHead.appendChild(NewsHead);
                div1.appendChild(panelHead);
                div1.style.marginTop = "10px";


                var div2 = document.getElementById("div2");
                div2.style.marginTop = "10px";
                div2.setAttribute("class", " col-md-9 col-md-offset-0 panel panel-primary");
                var panelHead = document.getElementById("NewsDesc");
                var Title = document.getElementById("Title");
                panelHead.setAttribute("class", "panel panel-heading");

                Title.appendChild(document.createTextNode("News"));

                panelHead.appendChild(Title);


                var ul = document.createElement("ul");
                var li = document.createElement("li");
                var a = document.createElement("a");

                **a.setAttribute("id",ntitle);**      ///   here i want to use 
                                                            ntitle object as id  
                                                            attribute of li   
                a.setAttribute("href", "#");
                a.appendChild(document.createTextNode(ntitle));
                li.appendChild(a);
                ul.appendChild(li);
                div1.appendChild(ul);

                cont.appendChild(div1);
                cont.appendChild(div2);


                $(document).ready(function () {

                    $("li").click(function () {

                        $("#cur[i].title").click(function () {

                            $("#h1").text(ntitle);
                            $("#p1").text(ncontent);
                            //p1.setAttribute("class", "jumbotron");
                        });

                    });


                });

                cont.appendChild(document.createElement("hr"));

            }
        }


    </script>

1 个答案:

答案 0 :(得分:0)

在for循环中,你不能只将li元素构造为字符串并使用jQuery的insert函数吗?