Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery选择在主干中的模板元素中不起作用_Javascript_Jquery_Templates_Backbone.js - Fatal编程技术网

Javascript Jquery选择在主干中的模板元素中不起作用

Javascript Jquery选择在主干中的模板元素中不起作用,javascript,jquery,templates,backbone.js,Javascript,Jquery,Templates,Backbone.js,我有一个简单的表单作为模板,还有一个初始化方法来根据需要更改textarea。问题是,我想针对textarea使用Jquery,但主干网不允许。例如,如果我想做this.$('textarea').css('height','1em')将在控制台中返回以下内容: [prevObject: jQuery.fn.jQuery.init[1], context: <form>, selector: "textarea"] context: <form> length: 0 pr

我有一个简单的表单作为模板,还有一个初始化方法来根据需要更改textarea。问题是,我想针对textarea使用Jquery,但主干网不允许。例如,如果我想做
this.$('textarea').css('height','1em')将在控制台中返回以下内容:

[prevObject: jQuery.fn.jQuery.init[1], context: <form>, selector: "textarea"]
context: <form>
length: 0
prevObject: jQuery.fn.jQuery.init[1]
selector: "textarea"
__proto__: Object[0]
views.js
会发生什么情况?

您不能在
initialize
函数中以这种方式修改HTML,因为模板还没有呈现出来--它找不到任何东西

我认为这样做的方法是把样式放在模板中,或者放在CSS文件中——这才是它真正的归属。但是,如果在加载视图时需要动态修改模板,则必须在编译模板之前进行修改。有关示例,请参见

startTextarea: function(){
    // get the template HTML
    var tmpl = $('#reply-form').html();

    // modify the template
    tmpl = $(tmpl).find('textarea').css('height', '1em')[0].outerHTML;

    // compile the template and cache to the view
    this.template = _.template(tmpl);
}

我就是这么想的。但是,如何操作通过模板创建的对象呢?即使我将此.render()添加到初始化中,也会得到相同的结果。@RobertSmith如果不能直接将其放入模板中,为什么不在
render
函数中执行呢?如果您必须直接修改模板,请参阅上面的我的更新。非常感谢您。。。但令人惊讶的是,它没有应用css。这很奇怪。@RobertSmith我并没有实际更新模板——请看我的更新。太好了!我想我想知道的大部分事情都可以通过事件来完成,所以我很高兴露营:-)
App.Views.Form = Backbone.View.extend({
    tagName: 'form',
    initialize: function() {
        this.startTextarea();
    },

    startTextarea: function(){
        console.log(this.$('textarea').css('height', '1em'));
    },

    template: _.template( $('#reply-form').html() ),

    render: function() {
        this.$el.html( this.template() );
        return this;
}
startTextarea: function(){
    // get the template HTML
    var tmpl = $('#reply-form').html();

    // modify the template
    tmpl = $(tmpl).find('textarea').css('height', '1em')[0].outerHTML;

    // compile the template and cache to the view
    this.template = _.template(tmpl);
}