Javascript 脚本将值解析到 JSP 页面上

Javascript Script parsing values onto JSP page

本文关键字:JSP 脚本 Javascript      更新时间:2023-09-26

所以我有一个Javascript脚本(不要评判它,我以前几乎没有使用过这项技术,这需要整洁!

$(document).ready(function()
{
    retrieveComments();
});

function retriveComments(){
        videoID = readCookie("currentVideoID");
        $.get("https://gdata.youtube.com/feeds/api/videos/" +videoID+ "/comments", function(d){
    $(d).find("entry").each(function(){
        var $entry = $(this);
        var author = $entry.attr("author");
        var comment = $entry.find("content").text();
        var html = '<div class="videoComments">';
        html += '<p class="author">" + author + "</p>';
        html += '<p class="comment"> " + comment + "</p>';
        html += '</div>'; 
   };
   $('#comments').append(html);
    });

我希望检索值作者和内容(评论(,并将其显示在页面上。我在一天中发现的示例显示了 2 个单独的文件,一个包含脚本,一个.jsp包含页面内容,然后是对应于最后一行的类标签 (?((在我的例子中是 #comments(。

看到除了我检索的数据和我在脚本中构建的内容之外,我不需要任何其他内容,我有这个:

<div id="comments"> </div>

但它没有显示,而且我看不到我的情况有什么不同。

我的整个页面看起来像:

<script type="text/javascript">
var videoID = readCookie("currentVideoID");
$(document).ready(function()
    {
        retrieveComments();
    });

function retriveComments(){
        videoID = readCookie("currentVideoID");
        $.get("https://gdata.youtube.com/feeds/api/videos/" +videoID+ "/comments", function(d){
        $(d).find("entry").each(function(){
            var $entry = $(this);
            var author = $entry.attr("author");
            var comment = $entry.find("content").text();
            var html = '<div class="videoComments">';
            html += '<p class="author">" + author + "</p>';
            html += '<p class="comment"> " + comment + "</p>';
            html += '</div>'; 
       };
       $('#comments').append(html);
       });
});
</script>
<h1>TEST</h1>
<div id="comments"> </div>

任何想法如何显示该 HTML?

不确定 readCookie 函数的作用(我假设它读取一个 cookie(,但这里它没有所有的语法错误并且正常工作:

$(document).ready(retriveComments);
function retriveComments() {
    $.get("https://gdata.youtube.com/feeds/api/videos/jofNR_WkoCE/comments", function (d) {
        $(d).find("entry").each(function (_, entry) {
            var author  = $(entry).find("author name").text(),
                comment = $(entry).find("content").text();
                html    = '<div class="videoComments">';
            html += '<p class="author">' + author + '</p>';
            html += '<p class="comment">' + comment + '</p>';
            html += '</div>';
            $('#comments').append(html);
        });
    });
}

小提琴