dsigg21445 2016-02-05 11:48
浏览 56
已采纳

动态更新列表中的数据

First post here on stack overflow.

I'm creating a YouTube stream monitor to keep an eye on multiple streams simultaneously and all was going well until i tried to work out how to update the data. Ive connected to the API, got the data into a json format and got the page working well. The problem now comes with updating the data and whilst i could just refresh the data section every x seconds that would cause the embedded video to refresh every time.

<?php
$url = "data_template.json"; //static json for development
$json = file_get_contents($url);
$data = json_decode($json);

$html = null;

foreach ($data->data as $program) {
    $html .= "<li id='1'>
              <img src='images/test.jpg' width='100%'>
              <!--YouTube Stream <iframe> embed -->
              <button>Public</button><button disabled>Preview - Not Enabled</button>
              <h4> {$program->broadcast->title} ( {$program->broadcast->id} )</h4>
              <p id='broad'>Broadcast Status: {$program->broadcast->lifeCycleStatus} </p>
              <p>Stream Status:  {$program->stream->streamStatus}  and  {$program->stream->healthStatus} </p>
              <p>Last Updated:  {$program->stream->lastUpdate} </p>
              </li>";
        };

echo $html;

?>   

I would like to update the variables within the paragraphs of each list but am struggling to work out how to get to them. Ive tried the below but it doesn't seem very efficient.

<script>
var x = document.getElementById("1");
    x.querySelector("#broad").innerHTML = "Hello World!";
</script>

Would i be better adding an intermediate step and having different variables in the HTML that can be individually ID'd and updated?

  • 写回答

1条回答 默认 最新

  • duanjipiao7076 2016-02-05 12:25
    关注
    • First, I would change your <li> tags to <div> tags, its more presentable and more concise with W3C standards.
    • Next you need to come up with the JS that would get the element you desire. You should highly consider jQuery. Its way easier to handle for a beginner like yourself.
    • Finally, when creating your HTML add a class to each element you will need to update with JS later.

    So for example if your HTML is like this:

    <script src="//code.jquery.com/jquery-1.10.2.js"></script><!-- jQuery lib -->
    
    <div class="video_block">
        <img src='images/test.jpg' width='100%'>
        <button>Public</button>
        <button disabled>Preview - Not Enabled</button>
        <h4><span class="title_1">Something</span> ( <span class="title_2">Something</span> )</h4>
        <p>Broadcast Status: <span class="b_status">Something</span> </p>
        <p>Stream Status:  <span class="s_status_1">Something</span>  and  <span class="s_status_2">Something</span> </p>
        <p>Last Updated:  <span class="u_status">Something</span> </p>
    </div>
    <div class="video_block">
        <img src='images/test.jpg' width='100%'>
        <button>Public</button>
        <button disabled>Preview - Not Enabled</button>
        <h4><span class="title_1">Something</span> ( <span class="title_2">Something</span> )</h4>
        <p>Broadcast Status: <span class="b_status">Something</span> </p>
        <p>Stream Status:  <span class="s_status_1">Something</span>  and  <span class="s_status_2">Something</span> </p>
        <p>Last Updated:  <span class="u_status">Something</span> </p>
    </div>
    

    Then your jQuery can be:

    <script>
       $('.video_block').each(function(){
           $(this).find('.title_1').text("something else");
           $(this).find('.s_status_1').text("something else");
           $(this).find('.u_status').text("something else");
       });
    </script>
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊