最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to generate li and anchor tag from list object? - Stack Overflow

programmeradmin2浏览0评论

Below is my output which i am getting from my web service but i want to create li tag from this:

{"d":[{"name":"ttt","url":"bbbb"},{"name":"uuu","url":"ppp"}]}

How to generate li tag from this above output??

I want li tag like this:

<li><a href="bbb" class="mylink">ttt</li>
<li><a href="uuu" class="mylink">ppp</li>

Below is my output which i am getting from my web service but i want to create li tag from this:

{"d":[{"name":"ttt","url":"bbbb"},{"name":"uuu","url":"ppp"}]}

How to generate li tag from this above output??

I want li tag like this:

<li><a href="bbb" class="mylink">ttt</li>
<li><a href="uuu" class="mylink">ppp</li>
Share Improve this question edited Dec 23, 2015 at 7:32 I Love Stackoverflow asked Dec 23, 2015 at 7:29 I Love StackoverflowI Love Stackoverflow 6,86822 gold badges113 silver badges238 bronze badges 1
  • @YeldarKurmangaliyev:see updated question – I Love Stackoverflow Commented Dec 23, 2015 at 7:32
Add a ment  | 

5 Answers 5

Reset to default 4

You should try this.

var obj = {
  "d": [{
    "name": "ttt",
    "url": "bbbb"
  }, {
    "name": "uuu",
    "url": "ppp"
  }]
};

$(obj.d).each(function(i,item) {
  $("<li/>").html($("<a>").attr("href",item.url).text(item.name)).appendTo('body');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js"></script>

Just use jQuery .each() to iterate through your object, and build your li using jQuery:

var obj = {
  "d": [{
    "name": "ttt",
    "url": "bbbb"
  }, {
    "name": "uuu",
    "url": "ppp"
  }]
};

$(obj.d).each(function() {
  var $a = $("<a/>").addClass('mylink').text(this.name).attr('href', this.url);
  $("<li/>").append($a).appendTo('body');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Please have a look at this approach:

var data = {"d":[{"name":"ttt","url":"bbbb"},{"name":"uuu","url":"ppp"}]};
$(document).ready(function(){
  var arrData = data.d;
  
  $.each(arrData, function( index, value ) {
   $("ul").append("<li><a href='"+value.url+"' class='mylink'>"+value.name+"</a></li>");
  });
  
  
});  
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div>
  <ul>
  </ul>  
</div>  

A solution in plain Javascript - step by step.

var object = { "d": [{ "name": "ttt", "url": "bbbb" }, { "name": "uuu", "url": "ppp" }] };

function generate(array) {
    var ul = document.createElement('ul');
    array.forEach(function (a) {
        var li = document.createElement('li'),
            anchor = document.createElement('a');
        anchor.href = a.url;
        anchor.text = a.name;
        li.appendChild(anchor);
        ul.appendChild(li);
    });
    return ul;
}

document.body.appendChild(generate(object.d));

You can do it without using jQuery.Also there is no data[i]['anchor'].I guess here you are looking for the url key.

HTML

<div id="wrapper"></div>

JS

(function(){
var x = {  
   "d":[  
      {  
         "name":"ttt",
         "url":"bbbb"
      },
      {  
         "name":"uuu",
         "url":"ppp"
      }
   ]
}
var o = x['d'];
var liElem="";
for(var a =0;a<o.length;a++){
liElem += '<li class="demoClass"><a href ="'+o[a].url+'">'+o[a].name+'</a></li>'
}
document.getElementById('wrapper').innerHTML=liElem
}())

WORKING MODEL

Hope this will be useful

发布评论

评论列表(0)

  1. 暂无评论