在jQuery中设置背景颜色?
问题描述:
我想做一个jQuery,如果值是130的背景颜色。在jQuery中设置背景颜色?
这是到目前为止我的代码:
function Compute(p, m, f, studno) {
var average;
average = parseFloat(p * 0.3) + parseFloat(m * 0.3) + parseFloat(f * 0.4);
$("#average_" + studno).val(average.toFixed());
if($(this).text()=="130"){
color:red
}
}
答
用这个代替 - $(this).css('backgroundColor', 'red');
:
function Compute(p, m, f, studno) {
var average;
average = parseFloat(p * 0.3) + parseFloat(m * 0.3) + parseFloat(f * 0.4);
$("#average_" + studno).val(average.toFixed());
if($(this).text()=="130"){
$(this).css('backgroundColor', 'red');
}
}
+0
您需要使用backgroundColor,因为它是javascript。 – 2013-04-08 09:15:43
答
要更改jQuery的背景颜色,你必须更改线路 “颜色:红”在你的代码中:
jQuery(this).css('background-color', 'red');
答
我觉得,函数Compute的调用不是基于对象E X:你可能调用Compute函数而不是任何ID或类$('#someID/Class')。Compute(....);因此,你不会在函数内获得$(this)关键字的值。因此无论哪种方式,你必须使用jquery插件的方法,或者因为你已经在你的问题中说过,当值为130时,考虑元素是否改变颜色,然后使用$(this).val()而不是$( this).text(),如果元素然后使用$(this).html()而不是$(this).text()。
如果您可以编辑您的答案并将代码格式化为更具可读性,那将会好很多。在编辑时查看帮助以了解如何。 – jakobhans 2013-04-08 09:11:58