如果出现 div 中的文本,请添加 class 或样式
Add class or style if text in div appears
我有这张正在制作的订单。
现在,当有人输入优惠券代码时,我想将旧价格的 div 应用于样式或 class(直通),虽然我做对了……这就是我我正在做......有什么建议为什么它可能不起作用吗?警告:我真的是 javascript 的新手......我一直在使用 CSS HTML 但 javascript 是另一回事。谢谢!
if ($('#grand_amount_value').length > 1){
$("#Total_Amount_old_val").attr("style", "text-decoration:line-through");
}
假设页面没有重新加载,您可以使用 setInterval 来检查元素是否存在。
var checkInterval = setInterval(function(){
if ($('#grand_amount_value').length){
$("#Total_Amount_old_val").attr("style", "text-decoration:line-through");
clearInterval(checkInterval);
}
},200);
最简单的清洁方式
$(document).on('change','#grand_amount_value',function(){
if ($('#grand_amount_value').val().length != 0){
$('#Total_Amount_old_val').css('text-decoration','line-through');
}
});
或者如果您需要持续检查
$(document).on('keypress','#grand_amount_value',function(){
if ($('#grand_amount_value').val().length != 0){
$('#Total_Amount_old_val').css('text-decoration','line-through');
}
});
这是我拼凑起来的一个例子来展示类似的工作:
https://jsfiddle.net/pj6xwhcy/3/
function strikeOldAmt() {
var gav = $("#grand_amount_value");
if (gav.length > 0 && gav.text() != "" ){
// There is a GAV element and it is not empty, strike out the old amount!
$("#Total_Amount_old_val").css("text-decoration", "line-through");
}
}
注意:使用 "style" 属性也会清除您可能添加的任何其他自定义样式。如果已设置样式属性,则使用 css()
函数将不会受到影响。
我有这张正在制作的订单。 现在,当有人输入优惠券代码时,我想将旧价格的 div 应用于样式或 class(直通),虽然我做对了……这就是我我正在做......有什么建议为什么它可能不起作用吗?警告:我真的是 javascript 的新手......我一直在使用 CSS HTML 但 javascript 是另一回事。谢谢!
if ($('#grand_amount_value').length > 1){
$("#Total_Amount_old_val").attr("style", "text-decoration:line-through");
}
假设页面没有重新加载,您可以使用 setInterval 来检查元素是否存在。
var checkInterval = setInterval(function(){
if ($('#grand_amount_value').length){
$("#Total_Amount_old_val").attr("style", "text-decoration:line-through");
clearInterval(checkInterval);
}
},200);
最简单的清洁方式
$(document).on('change','#grand_amount_value',function(){
if ($('#grand_amount_value').val().length != 0){
$('#Total_Amount_old_val').css('text-decoration','line-through');
}
});
或者如果您需要持续检查
$(document).on('keypress','#grand_amount_value',function(){
if ($('#grand_amount_value').val().length != 0){
$('#Total_Amount_old_val').css('text-decoration','line-through');
}
});
这是我拼凑起来的一个例子来展示类似的工作:
https://jsfiddle.net/pj6xwhcy/3/
function strikeOldAmt() {
var gav = $("#grand_amount_value");
if (gav.length > 0 && gav.text() != "" ){
// There is a GAV element and it is not empty, strike out the old amount!
$("#Total_Amount_old_val").css("text-decoration", "line-through");
}
}
注意:使用 "style" 属性也会清除您可能添加的任何其他自定义样式。如果已设置样式属性,则使用 css()
函数将不会受到影响。