使用jQuery来回显文本

时间:2010-04-01 17:16:39

标签: javascript jquery

是否可以使用jQuery来代替脚本标记来回显文本?更准确地说,有没有办法实现

<script type="text/javascript">
    document.write("foo");
</script>

...没有使用document.write?阅读this后,我对使用document.write感到高兴。

我知道我可以选择这样做:

<span id="container"></span>
<script type="text/javascript">
    $("#container").text("foo");
</script>

但是,我有兴趣看看是否有办法在不使用容器元素的情况下进行,最好使用jQuery。

提前致谢!

3 个答案:

答案 0 :(得分:6)

如果你想出一种jQuery方式来做document.write(),那么出于同样的原因它会很糟糕。

你最好只使用document.write(),如果这是你需要的,或者更好的是,操纵一个现有元素或在DOM中的某个地方附加一个新元素 - 这就是jQuery擅长的。

答案 1 :(得分:1)

是的,没有。你想要什么,没有。

  1. 你能不能将文本回显到没有真正容器的东西,是的(参见:DocumentFragment)。

  2. 它会出现在你的文件中吗......没有。这是因为没有告诉它应该放在哪里。 html中的脚本标签不会直接保持其作为参数的位置,因此您可以捏造以找到最后一个标签并将TextNode放在那里,但是,这可能很难并且有问题。

  3. 您可以做的是在“document.body.onLoad”之类的事件之前不要修改dom的一般做法。这是一种常见的做法,通常是特别适合ajax的方法。

    如果这些都不适合您,请使用罕见的insertBefore(),jquery在带有id的脚本元素上提供与.after和.before类似的支持。

    <script id="flail">
    var flail=document.getElementById("flail");
    flail.parentNode.insertBefore(document.createTextNode("TEST"),flail)
    </script>
    

    注意:这通常是一种不好的做法,因为它可以创建悬挂负载,并鼓励html页面在没有此输出的情况下不一致。然而,就像所有事情一样,有些情况可供使用。

答案 2 :(得分:-1)

我建议您通过jquery创始人John Resig实施微模板引擎。

完整插件

// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function() {
    var cache = {};

    this.tmpl = function tmpl(str, data) {
        // Figure out if we're getting a template, or if we need to
        // load the template - and be sure to cache the result.
        var fn = !/\W/.test(str) ?
                cache[str] = cache[str] ||
                tmpl(document.getElementById(str).innerHTML) :
                // Generate a reusable function that will serve as a template
                // generator (and which will be cached).
                new Function("obj",
                "var p=[],print=function(){p.push.apply(p,arguments);};" +
                // Introduce the data as local variables using with(){}
                "with(obj){p.push('" +
                // Convert the template into pure JavaScript
                str
                .replace(/[\r\t\n]/g, " ")
                .split("<%").join("\t")
                .replace(/((^|%>)[^\t]*)'/g, "$1\r")
                .replace(/\t=(.*?)%>/g, "',$1,'")
                .split("\t").join("');")
                .split("%>").join("p.push('")
                .split("\r").join("\\'")
                + "');}return p.join('');");

        // Provide some basic currying to the user
        return data ? fn(data) : fn;
    };
})();

使用

重要提示:仅使用\

分隔行
var tpl = '\
    <div id="myTemplate">\
        <%\ var selectorIndex = 0;\ %>\
           <ul>\
                <% if( selectorIndex == 0 ){ %>\
                <li>this is echo text for zero</li>\
                <% } else{ %>\
                <li>this is echo text for something else</li>\
                <% } %>\
           </ul>\
    </div>\
';

$(body).html(tmpl(tpl,{'extraData':'here'}));

更多信息

http://krasimirtsonev.com/blog/article/Javascript-template-engine-in-just-20-line

有关stackoverflow的相关问题

Syntax Error with John Resig's Micro Templating after changing template tags <# {% {{ etc