[英]How can I make JS objects into jQuery elements?
I'll admit that I don't really know what I'm doing. 我得承认,我真的不知道我在做什么。 But I'm trying to work with JS in an OO way.
但我正试图以OO的方式使用JS。
I have a form field where I input data. 我有一个输入数据的表单域。 On the right side I have a "preview" of how the special offer will look like.
在右侧,我有一个“预览”特别优惠的样子。
As I add text in the input fields, I want it to show in the preview. 当我在输入字段中添加文本时,我希望它在预览中显示。
I've tried using the following code, but I'm not being very successful. 我尝试使用以下代码,但我不是很成功。 I think there are some basic knowledge / understanding I'm missing here.
我认为我在这里缺少一些基本的知识/理解。
mirrorText: function(){
var self = this;
$('#title').keyup(function(){
self.previewObject.title.text($(this).val());
});
},
previewObject: {
img: null,
title: null,
dateRange: null,
address: null,
init: function(){
self.img = $('.offer-container .promo-img');
self.title = $('.offer-container h3');
self.dateRange = $('.offer-container .valid-through');
}
}
See the full code here (jsfiddle). 请参阅此处的完整代码 (jsfiddle)。
You can't have DIVs inside a P
(paragraph) so the elements were not found. 您不能在
P
(段落)中包含DIV,因此找不到元素。 I changed that to another div
for the example. 我将其更改为另一个
div
作为示例。
You also referenced self
in one place where I think it should have been this (inside init). 你还在一个地方引用了
self
,我认为应该是这个 (在init中)。
JSFiddle: http://jsfiddle.net/TrueBlueAussie/w0nn5Ls9/6/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/w0nn5Ls9/6/
Promo = {
init: function(){
var self = this;
self.previewObject.init();
self.mirrorText();
},
mirrorText: function(){
var self = this;
$('#title').keyup(function(){
self.previewObject.title.html($(this).val());
});
},
previewObject: {
img: null,
title: null,
dateRange: null,
address: null,
init: function(){
this.img = $('.offer-container .promo-img');
this.title = $('.offer-container h3');
this.dateRange = $('.offer-container .valid-through');
}
}
}
Promo.init();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.