Difficulty adding object as innerHTML to a dynamically created div in javascript/jquery

后端 未结 1 920
天涯浪人
天涯浪人 2021-01-26 02:32

I have below code in my project

HTML:




        
相关标签:
1条回答
  • You can use jQuery's grep function which is intended for searching an array.

    function getObjContents(i){
      var arr = $.grep(input, function(e){ 
        return e.id == i; 
      });
      var str="";
      for (var i = 0; i < arr[0].st.length; i++) {
        str += JSON.stringify(arr[0].st[i]);
      }
      return str;
    }
    

    You would call it like this:

    $( "<div id='sp"+array[i]+"'>This is div with id "+array[i]+getObjContents(Num)+"<div class='st'></div></div>" ).insertAfter( "#main");
    

    Demo

    0 讨论(0)
提交回复
热议问题