如果div中的文字显示

时间:2016-07-25 15:53:48

标签: javascript jquery html styles

我有这个订单,我正在制作。 现在当有人把优惠券代码我想要应用于旧价格的div时,风格或类别(直通)和我虽然我说得对......这就是我正在做的...... ..有什么建议为什么它可能不起作用?警告:我是javascript的新手....我一直在使用CSS HTML,但javascript是另一个故事。谢谢!

if ($('#grand_amount_value').length > 1){
    $("#Total_Amount_old_val").attr("style", "text-decoration:line-through");
}

3 个答案:

答案 0 :(得分:0)

假设页面没有重新加载,您可以使用setInterval来检查元素是否存在。

var checkInterval = setInterval(function(){
if ($('#grand_amount_value').length){
    $("#Total_Amount_old_val").attr("style", "text-decoration:line-through");
    clearInterval(checkInterval);
}
},200);

答案 1 :(得分:0)

最简洁的方式

$(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');
        }
    });

Here a jsfiddle

答案 2 :(得分:0)

这是一个例子,我把它拼凑在一起,以显示这样的工作:

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”属性也会消除您可能添加的任何其他自定义样式。如果已设置style属性,则使用css()函数不会发生干扰。