将变量的值从javascript打印到html

时间:2018-08-12 08:16:54

标签: javascript html json

如何在ministries()中打印变量count的值。例如我有54个对象。它将显示ministries(54)。我如何在部委中打印54(count的值)。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Data Retrieve</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <style>
        #p1    {background-color: violet;}
    </style>
</head>
<body>
  <div class="container">
    <div class="row">
      <table id="info_table" class="table">
        <tr>
          <td><p align="center"><b>Ministry</b></p></td>
        </tr>
        <tr id="p1">  
          <td><p align="center"><b>Total ministries()</b></p></td>
        </tr>
      </table>      
    </div> 
  </div>
</body>
</html>  
<script>
    $.getJSON("http://localhost/directory/ministri.json",function(obj){
        var info ='';
        //alert(obj.data.length);
        var count = obj.data.length;
        $.each(obj.data,function(key,value){

            info += '<tr>';
            info +='<td><a href="http://localhost/directory/show.php?id='+value.id+'" style="text-decoration:none">'+value.sitename_bn+'<span style="float: right"> > </span>'+ '</a></td>';
            info += '</tr>';

        });

           $('#info_table').append(info);
    });
</script>

2 个答案:

答案 0 :(得分:0)

将表格结构更改为

<table id="info_table" class="table">
    <thead>
         <tr>
      <td><p align="center"><b>Ministry</b></p></td>
    </tr>
    <tr id="p1">  
      <td><p align="center"><b>Total ministries()</b></p></td>
    </tr>
   </thead>
   <tbody>
   </tbody>
  </table>   

,然后将jQuery代码更改为

$('#info_table > tbody').append(info);

答案 1 :(得分:0)

这真的很简单。只需定位您的元素 <div role="tabpanel" class="tab-pane fade " id="tab3"> <div class="circular-div"> <table class="table table-striped table-hover" id="circular-table"> <thead> <tr> <th>ردیف</th> <th>دسته بندی</th> <th>عنوان</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>بخشنامه‌ها</td> <td>اطلاعیه جهاد دانشگاهی</td> </tr> <tr> <td>2</td> <td>بخشنامه‌ها</td> ... ... ... 并将其HTML替换为您获得的值即可。

#p1