javascript - 我怎样才能做同样的事情,但在 HTML 而不是 JavaScript 对话框中?

标签 javascript jquery ajax

我的代码允许在耗时的同时弹出alert对话框。

如何在 HTML 而不是 JavaScript 对话框中做同样的事情?

我必须用与 AJAX 相关的东西来做吗?

有人可以给我举个例子吗?

jQuery(document).ready(function () {
    var comments = [{'time':'5','message':'hello! 5 secs has past'},{'time':'10','message':'hello! 10 secs has past'},{'time':'30','message':'hello! 30 secs has past'}];

    $('#video').on('timeupdate',function(e){
        showComments(this.currentTime);
    });

    function showComments(time){
        var comments = findComments(time);
        $.each(comments,function(i,comment){
            alert(comment.message);
        });
    }

    function findComments(time){
        return $.grep(comments, function(item){
          return item.time == time.toFixed();
        });
    }

});

HTML

<html>
 <head>
  <title>Test</title>
 </head>
 <body>
  <h1>Show sequential messages in HTML</h1>
  <p>
  **Messages appears here instead of dialog**
  </p>
 </body>

最佳答案

在您的 showComments 函数中,替换这一行:

alert(comment.message);

进入这些:

var messages = $('p').text();
$('p').text(messages + comment.message + "\n");

// Show for 5 seconds, then hide the `p` element.
$('p').show().delay(5000).fadeOut();

这样,它将用评论消息填充段落。

关于javascript - 我怎样才能做同样的事情,但在 HTML 而不是 JavaScript 对话框中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18347841/

相关文章:

javascript - 如何防止 setInterval() 倍增?

javascript - 如何在主干中实现 Angular 样式指令?

Jquery 改变输入值

javascript - 使用 html5 通过 ajax 和 jquery 上传文件

jquery - 在 jQuery UI 对话框中触发按钮单击

java - 在 Java EE 中使用 Ajax 传递 JSON 字符串

javascript - 如何从异步调用返回响应?

javascript - 更改具有相同 onclick 类的多个 div 中的文本

javascript - React Native FlatList 水平模式根本不起作用

JQuery Ajax 调用,类型为 : "POST" is received as "GET" by server