【问题标题】:Convert console.log to output to a div将 console.log 转换为输出到 div
【发布时间】:2016-09-14 10:49:03
【问题描述】:

我正在使用本主题中的简单 rss 提要解析器示例 = Rss Parser Example

$.get('https://stackoverflow.com/feeds/question/10943544', function (data) {
    $(data).find("entry").each(function () { // or "item" or whatever suits your feed
        var el = $(this);

        console.log("------------------------");
        console.log("title      : " + el.find("title").text());
        console.log("author     : " + el.find("author").text());
        console.log("description: " + el.find("description").text());
    });
});

它很完美,但是我想在特定的 div 中显示每个输出日志,例如:

标题日志进入<div class="Rss-Title"></div> 等等。所以最后我应该有这样的东西:

<div id="Rss">

<div class="Rss-Title"></div>

<div class="Rss-Author"></div>

<div class="Rss-Description"></div>

</div>

我的问题:做这样的事情的正确方法是什么?

【问题讨论】:

  • 你想在 div 中做日志条目吗?
  • 不清楚:可以换源吗?您的标题+问题不匹配。
  • 很抱歉,我的人很清楚。 Ish alrdy 发布了答案。
  • 不用担心 - 很高兴您得到了答案。我冒昧地更新了问题标题,以更好地反映问题以供将来搜索。

标签: jquery html rss


【解决方案1】:

你应该使用.html()函数

<div id="Rss">

<div class="Rss-Title"></div>

<div class="Rss-Author"></div>

<div class="Rss-Description"></div>

</div>

$.get('http://stackoverflow.com/feeds/question/10943544', function (data) {
 $(data).find("entry").each(function () { // or "item" or whatever suits your feed
    var el = $(this);

    $('#Rss-Title').html(el.find("title").text());
    $('#Rss-Author').html(el.find("author").text());
    $('#Rss-Description').html(el.find("description").text());
 });
});

【讨论】:

  • 这就是我的意思。谢谢!
  • 很高兴为您提供帮助:)
【解决方案2】:

您可以使用html()(或作为替代text())选择元素并在其中放置文本,而不是console.log

$.get('http://stackoverflow.com/feeds/question/10943544', function (data) {
    $(data).find("entry").each(function () { // or "item" or whatever suits your feed
        var el = $(this);
        $('.Rss-Title').html("title      : " + el.find("title").text());
        $('.Rss-Author').html("author     : " + el.find("author").text());
        $('.Rss-Description').html("description: " + el.find("description").text());
    });
});

【讨论】:

  • 谢谢,会尝试两者 - urs 和 Ish's。
【解决方案3】:

从标题/问题中有点不清楚问题是什么,所以这个答案是为了:

Console.log 到一个 div

执行此操作的“正确”方法是更改​​执行 console.log 的源

要将console.log改为div,可以重写console.log,例如:

console.log("test1")
window.console.log = function(txt) { 
    //alert(txt)
    $("#log").text($("#log").text() + txt);
}
console.log("test2")

【讨论】:

  • 抱歉不清楚。 Ish alrdy 发布了答案。这就是我的意思。
猜你喜欢
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 2012-02-13
  • 2016-08-09
  • 2011-07-15
  • 2012-01-23
  • 2017-04-04
相关资源
最近更新 更多