Javascript 使用jquery操作html的某些部分

Javascript 使用jquery操作html的某些部分,javascript,jquery,html,Javascript,Jquery,Html,在我的html中有一个(隐藏的)模板,类似 <div class="tabletemplate dontshow" > <p class="movieFile"></p> <p class="movieDirector"></p> </div> 然后我想在模板中插入文本字符串。我试过一些东西,比如 tableTemplate.find(".movieFile").text(movieInfo.FileName)

在我的html中有一个(隐藏的)模板,类似

<div class="tabletemplate dontshow" >
   <p class="movieFile"></p>
   <p class="movieDirector"></p>
</div>
然后我想在模板中插入文本字符串。我试过一些东西,比如

tableTemplate.find(".movieFile").text(movieInfo.FileName);

但似乎都不管用


理想情况下,我怎样才能轻松地做到这一点,而不必学习整个新的js库。

最好的解决方案是函数appendTo:$(“.movieFile”).appendTo(movieInfo.FileName); $(".movieFile").appendTo(movieInfo.FileName); 或

tableTemplate.find(“.movieFile”).appendTo(movieInfo.FileName) 试试这个:

$(".tabletemplate").clone().removeClass("dontshow").appendTo(".tabletemplate");
var movieFile = $(".tabletemplate").find(".movieFile")[0];
$(movieFile).text(movieInfo.FileName);

对我有用。当然,您必须将最后一个appendTo类更改为tabletemplate的父类。

看起来我一直都是对的,只是我的
movieInfo.FileName
对象由于某种原因无法工作,尽管firebug向我显示了正确的字符串。我还得继续努力。用文本常量替换它表明我走上了正确的道路。。。 原来我不需要
“append”
“appendTo”

以下是我所拥有的,它是有效的:

    jQuery.fn.outerHTML = function () {
        return (this[0]) ? this[0].outerHTML : '';
    };

    function createTableElement(movieInfo) {
        var template = tableTemplate.clone();

        // template.find(".movieFile").text(movieInfo.FileName);
        template.find(".movieFile").text("the movie");
        template.find(".movieDirector").text("the director");
        return template.outerHTML();
    }
此函数返回整个
div
的html字符串,然后我可以使用该字符串构造表


感谢大家的帮助和快速回复

插入文本文件后。。。您是否将克隆的元素追加回DOM克隆后,您需要在某处插入该tableTemplate。请参见“是”,我将该模板转换为文本字符串,然后将其插入到我动态创建的表中。这一切都有效。我和firebug核实过了。模板很好,movieInfo.FileName有一个字符串,我可以从模板创建表。在创建表之前,我无法将字符串放入模板中。请在我的代码上下文中为我提供解决方案?我对jquery非常陌生。$(“.movieFile”).appendTo(movieInfo.FileName);否。表格模板未更改。我会在几个小时后继续,然后报告。好的,我很确定你使用了appendTo,我几天前也做了类似的事情。 tableTemplate.find(".movieFile").appendTo(movieInfo.FileName);
$(".tabletemplate").clone().removeClass("dontshow").appendTo(".tabletemplate");
var movieFile = $(".tabletemplate").find(".movieFile")[0];
$(movieFile).text(movieInfo.FileName);
    jQuery.fn.outerHTML = function () {
        return (this[0]) ? this[0].outerHTML : '';
    };

    function createTableElement(movieInfo) {
        var template = tableTemplate.clone();

        // template.find(".movieFile").text(movieInfo.FileName);
        template.find(".movieFile").text("the movie");
        template.find(".movieDirector").text("the director");
        return template.outerHTML();
    }