DOM就绪时的Helper函数

Helper Function when DOM ready

本文关键字:Helper 函数 就绪 DOM      更新时间:2023-09-26

我目前正在写博客,添加新文章时需要一个预览窗口。所以我有一个自动表单,你可以在那里输入新的文章:

<template name = "AddArticle">
<div class = "content-container">
    <div class = "content-wrapper">
        <h2>Add new Article</h2>
        <!--{{> quickForm id="insertArticleForm" type = "insert"}}-->
        {{#autoForm collection="BlogPosts" id="insertArticleForm" type="insert" class = "content-form"}}
            <fieldset>
                {{> afQuickField name="author"}}
                {{> afQuickField name="title"}}
                {{> afQuickField name="description"}}
                {{> afQuickField name="content" rows=15 id = "content-textarea"}}
                <button type="submit" class="btn btn-primary">Insert</button>
            </fieldset>
        {{/autoForm}}
    </div>
</div>
{{> Preview}}

我有这个预览窗口

<template name = "Preview">
<div class = "content-container preview">
    {{>SlideBlock title = 'Preview <button class ="btn btn-default pull-right">Refresh</button>' content = "PreviewContent"}}
</div>

<template name = "PreviewContent">
<div id = "preview-content" class = "content-wrapper">
    <span>{{{content}}}</span>
</div>

这个Helper函数应该将文本从文本区域复制到预览窗口

Template.PreviewContent.helpers({
   content: function(){
    var content = $("#content-textarea");
    console.log(content);
    return content;
   } 
});

问题是,当前输出只是[object object]。我相信可能是在dom还没有准备好的时候执行了helper功能,但我不能100%确定。

编辑:

哦,试过东西后忘了再加一次。带有.html()/.val()/.text()的输出都只是返回未定义。

[object object]并不意味着DOM还没有准备好,而且您的API似乎需要内容而不是对象本身。所以试试

var content = $("#content-textarea").html();

var content = $("#content-textarea").val();