如何在两个javascript对象文本之间进行通信?
我想知道如何从另一个对象文本中的对象调用方法。这是我的密码。我正在构建一个jQueryUI滑块。我试图调用动画对象文字中的animatebox函数,我的chrome控制台中的错误是:如何在两个javascript对象文本之间进行通信?,javascript,jquery-ui-slider,Javascript,Jquery Ui Slider,我想知道如何从另一个对象文本中的对象调用方法。这是我的密码。我正在构建一个jQueryUI滑块。我试图调用动画对象文字中的animatebox函数,我的chrome控制台中的错误是: Uncaught TypeError: Cannot call method 'animateBox' of undefined 我的代码如下所示: var sliderOpts = { animate: true, range: "min", value: 50, min: 1
Uncaught TypeError: Cannot call method 'animateBox' of undefined
我的代码如下所示:
var sliderOpts = {
animate: true,
range: "min",
value: 50,
min: 10,
max: 100,
step: 10,
//this gets a live reading of the value and prints it on the page
slide: function (event, ui) {
$("#slider-result").html(ui.value + " GB");
},
//this updates the hidden form field so we can submit the data using a form
change: function (event, ui) {
$('#hidden').attr('value', ui.value);
var val = ui.value,
$box = $('#message');
switch (true) {
case (val > 50):
$box.animations.animateBox().html('you have chosen xtremenet');
break;
case (val < 50):
$box.fadeOut().fadeIn().html('you have chosen valuenet');
break;
default:
$box.fadeOut().fadeIn().html('you have chosen powernet');
}
}
};
var animations = {
animateBox: function () {
$("#message").slideDown(500);
}
};
$(".slider").bind("slidecreate", animations.animateBox).slider(sliderOpts);
var sliderOpts={
动画:对,
射程:“分钟”,
价值:50,
民:10,,
最高:100,
步骤:10,
//这将实时读取值并将其打印到页面上
幻灯片:功能(事件、用户界面){
$(“#滑块结果”).html(ui.value+“GB”);
},
//这将更新隐藏的表单字段,以便我们可以使用表单提交数据
更改:功能(事件、用户界面){
$('#hidden').attr('value',ui.value);
var val=ui.value,
$box=$(“#消息”);
开关(真){
病例(val>50):
$box.animations.animateBox().html('您选择了xtremenet');
打破
病例(val<50):
$box.fadeOut().fadeIn().html('您已选择valuenet');
打破
违约:
$box.fadeOut().fadeIn().html('您选择了powernet');
}
}
};
变量动画={
animateBox:函数(){
$(“#消息”)。向下滑动(500);
}
};
$(“.slider”).bind(“slidecreate”,animations.animateBox).slider(sliderOpts);
那么,我如何将这个方法称为animateBox呢?
$box.animations
-是未定义的-事实就是如此
id=“message”为jQuery对象包装的元素没有animations属性
在您的情况下,您可以通过简单地调用
animations.animateBox();
而不是
$box.animations.animateBox()
$box.animations
-是未定义的-就是这样
id=“message”为jQuery对象包装的元素没有animations属性
在您的情况下,您可以通过简单地调用
animations.animateBox();
您可以通过选项提供animateBox函数作为回调函数,而不是
$box.animations.animateBox()
。像这样:
var sliderOpts = {
animateBox: function() { // magic here },
animate: true,
range: "min",
...
};
然后在插件中设置滑块的animateBox值。如果使用标准插件约定,则调用“动画改变中的长方体”如下所示:
this.animateBox();
您可以通过选项提供animateBox函数作为回调函数。像这样:
var sliderOpts = {
animateBox: function() { // magic here },
animate: true,
range: "min",
...
};
然后在插件中设置滑块的animateBox值。如果使用标准插件约定,则调用“动画改变中的长方体”如下所示:
this.animateBox();
jquery对象$box没有.animations属性。您已将动画定义为全局(窗口级别)变量 链接也不起作用,因为您不从animateBox函数返回任何内容。我猜您希望将该函数更改为返回$(“#message”)。一旦你有了它,而不是
$box.animations.animateBox().html('you have chosen xtremenet');
试一试
jquery对象$box没有.animations属性。您已将动画定义为全局(窗口级别)变量 链接也不起作用,因为您不从animateBox函数返回任何内容。我猜您希望将该函数更改为返回$(“#message”)。一旦你有了它,而不是
$box.animations.animateBox().html('you have chosen xtremenet');
试一试
您应该能够调用
animations.animateBox()
。您尚未将其设置为jQuery方法,因此引发错误的可能是$box.animations
。您应该能够调用animations.animateBox()
。您还没有将其设置为jQuery方法,因此引发错误的可能是$box.animations
。我不明白这个错误,我无法想象您为什么会这样做。代码看起来不错(假设“slider”是某个插件;我想可能是jqueryui)。我不确定sliderOpts中的change方法中对animateBox的调用。我不知道$box.animations.animateBox会变成什么样子assigned@Pointy是的。代码对您来说合适吗?可以从一个对象在另一个对象中调用函数吗?我不需要初始化吗?@tinyd是的。这就是问题所在。你能告诉我正确的方法吗?@tinyd啊,是的,我错过了“更改”处理程序中的呼叫。我不明白这个错误,我无法想象你为什么会这样做。代码看起来不错(假设“slider”是某个插件;我想可能是jqueryui)。我不确定sliderOpts中的change方法中对animateBox的调用。我不知道$box.animations.animateBox会变成什么样子assigned@Pointy是的。代码对您来说合适吗?可以从一个对象在另一个对象中调用函数吗?我不需要初始化吗?@tinyd是的。这就是问题所在。你能告诉我正确的方法吗?@tinyd啊,是的,我错过了“change”处理程序中的调用。如果你让animateBox返回$(“#message”).slideDown(500),而不是仅仅调用它,你应该能够在它上调用.html()。动画可能有点复杂,如OP所示(或者创建单独对象文字的原因是什么),但您是对的,元素本身可以返回以进行进一步链接,如果您使animateBox返回$(“#message”).slideDown(500)而不是仅调用它,您应该能够在其上调用.html()。动画可能有点复杂,如OP所示(或者创建单独对象文字的原因是什么),但您是对的,元素本身可以返回以进行进一步链接。这非常接近。我必须更改为$(obj)。slideDown(500);我现在得到的错误是:Uncaught TypeError:无法使用'in'运算符在True中搜索'fxqueue fx.run'。您可以执行$.fn.animateBox=函数(){返回this.each(function(){$(this.slideDown(500)});};然后返回$box.animateBow().html(“Blah Blah”);那么错误可能是因为动画的定义应该在slideoptsnotworkman上面。我得到的错误是:uncaughttypeerror:Object function(){返回this.each(function(){$(this.slideDown(500)});}没有方法“html”嘿
var animations = {
animateBox: function (obj) {
obj.slideDown(500);
}
};
animations.animateBox($box.html('you have chosen xtremenet'))