Javascript 如何使用jQuery插件访问$(this)
我正在使用jqueryrating,在将成功消息显示到相应的div中时遇到问题,因为$(this)不引用选择器,而是返回Object 这是我的密码:Javascript 如何使用jQuery插件访问$(this),javascript,jquery,plugins,selector,this,Javascript,Jquery,Plugins,Selector,This,我正在使用jqueryrating,在将成功消息显示到相应的div中时遇到问题,因为$(this)不引用选择器,而是返回Object 这是我的密码: $(".basic").jRating({ bigStarsPath: 'assets/stars.png', rateMax: 5, length : 5, phpPath : '<%= Rails.application.class.routes.url_helpers.ratings_pat
$(".basic").jRating({
bigStarsPath: 'assets/stars.png',
rateMax: 5,
length : 5,
phpPath : '<%= Rails.application.class.routes.url_helpers.ratings_path %>',
onSuccess : function(){
$(this).find(".message").append('<small>Thanks for your vote!</small>');
}
});
$(“.basic”).jRating({
bigStarsPath:'assets/stars.png',
最高比率:5,
长度:5,
phpPath:“,
onSuccess:function(){
$(this.find(“.message”).append('谢谢您的投票!');
}
});
所以问题是$(this)没有引用$(“.basic”),我想知道如何访问插件中的选择器。
我很确定这可能是个新问题,但我的javascript技能一点也不全面。
提前谢谢。成功
的第一个参数是元素。用那个
onSuccess : function(element){
$(element).find(".message").append('<small>Thanks for your vote!</small>');
}
onSuccess:函数(元素){
$(元素).find(“.message”).append('谢谢您的投票!');
}
由插件将this
设置为某物,在这种情况下,插件将onSuccess
函数与this
一起触发,作为传递给jRating
的选项,而不是元素。onSuccess:function(元素,速率){
onSuccess : function(element, rate){
$(element).find(".message").append('<small>Thanks for your vote!</small>');
}
$(元素).find(“.message”).append('谢谢您的投票!');
}
您可以在成功选项中访问元素和/或比率。var elem=$(此选项);
var elem = $(this);
$(".basic").jRating({
bigStarsPath: 'assets/stars.png',
rateMax: 5,
length : 5,
phpPath : '<%= Rails.application.class.routes.url_helpers.ratings_path %>',
onSuccess : function(){
elem.find(".message").append('<small>Thanks for your vote!</small>');
}
});
$(“.basic”).jRating({
bigStarsPath:'assets/stars.png',
最高比率:5,
长度:5,
phpPath:“,
onSuccess:function(){
elem.find(“.message”).append('谢谢您的投票!');
}
});
关于jRating源代码,将传递给onSuccess和onError处理程序的参数是元素和评级,因此解决方案是:
$(".basic").jRating({
bigStarsPath: 'assets/stars.png',
rateMax: 5,
length : 5,
phpPath : '<%= Rails.application.class.routes.url_helpers.ratings_path %>',
onSuccess : function(element /*, rating */){
$(element).find(".message").append('<small>Thanks for your vote!</small>');
}
});
$(“.basic”).jRating({
bigStarsPath:'assets/stars.png',
最高比率:5,
长度:5,
phpPath:“,
onSuccess:功能(元素/*,评级*/){
$(元素).find(“.message”).append('谢谢您的投票!');
}
});
对象对象对象是将对象转换为字符串时得到的对象。正在尝试使用警报($(this))
进行调试?