$(this)in javascript
作者:互联网
我在js中使用它时遇到问题.我有一个函数使用这样的:
var refreshRequesterStar = function() {
$(".rating").raty({
score: function() { return $(this).attr('data-rating'); },
readOnly: function() { return $(this).attr('data-readonly'); },
halfShow: true
});
};
等级div如下:
<div class="rating" data-readonly="false" data-rating="3.0" style="cursor: default; width: 100px;" title="not rated yet">
<div class="rating" data-readonly="false" data-rating="0.0" style="cursor: default; width: 100px;" title="not rated yet">
这由以下函数调用:
$("body").ajaxComplete(function(){
refreshRequesterStar();
$(".time_to_expire").each(function(){
setCountDown(this);
})
$('select').chosen();
});
我可以设置得分值,但不能设置readOnly值.当我使用firebug调试时,我发现第一个指向div,第二个指向window.我哪里错了?注意:我对JavaScript不太了解.
更多信息:我在使用带有导轨的Raty http://www.wbotelhos.com/raty.
解决方法:
文档和示例表明,参数score和readOnly应该是数字和布尔值,而不是回调函数.您可能要重新编写代码:
$(".rating").each(function() {
// inside the function, this refers to the .rating element being iterated
$(this).raty({
score: parseFloat($(this).attr('data-rating')), // see note #1
readOnly: $(this).attr('data-readonly') == "true", // see note #2
halfShow: true
});
});
> .attr()返回一个字符串; parseFloat()
函数用于转换字符串,例如“ 2.5”变成数字2.5
> ==“ true”如果属性值等于“ true”,则返回布尔值true;否则,返回true.在其他所有情况下均返回false
参考:
> jQuery.each()
> jQuery.attr()
标签:raty,ruby-on-rails-3,javascript,jquery 来源: https://codeday.me/bug/20191201/2078736.html