1

我提前为发布另一个IE 不透明度问题而道歉,但我已经阅读了至少 30 个不同的页面(很多在 SO 上)并且无法使其工作。

我正在尝试根据用户在卡号字段中输入的第一个字符来动态更改美国 4 种主要信用卡类型(Visa、MC、Amex、Discover)的不透明度<input>

我的代码在 IE9、Firefox、Safari 和 Chrome 中运行良好,但当然不是 IE8,这是 Windows XP 支持所必需的。

我尝试使用以下设置:

object.style.filter = 'alpha(opacity=13)';
object.filter = 'alpha(opacity=13)';
object.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=13)';

我什至尝试将 O 中的 Opacity 大写,没想到它会有所帮助......

有谁知道我错过了什么?

一个 JQuery 解决方案很好,但我想知道出于学术原因在 Javascript 中是否有可能......

我有以下 HTML:

<span>
  <input name='cardnumber' value="3717XXXXXXX8775" type='text' id='cardnumber' onkeyup='setCardType();'>
</span>
<span>
  <img src='/style/icon_visa.gif' id='visa' alt='This is a Visa' style='opacity:.13;filter:alpha(opacity=13);'>&nbsp;&nbsp;
  <img src='/style/icon_mastercard.gif' id='mastercard' alt='This is a MasterCard' style='opacity:.13;filter:alpha(opacity=13);'>&nbsp;&nbsp;
  <img src='/style/icon_amex.gif' id='amex' alt='This is an American Express' style='opacity:1;filter:alpha(opacity=100);'>&nbsp;&nbsp;
  <img src='/style/icon_discover.gif' id='discover' alt='This is a Discover Card' style='opacity:.13;filter:alpha(opacity=13);'>
</span>
<input type='hidden' name='cardtype' id='cardtype' value="American Express">

和以下Javascript:

function setCardType() {
var cardnumber = document.getElementById('cardnumber').value;
cardnumber = cardnumber.replace(/[^0-9]/g,'');
document.getElementById('cardnumber').value = cardnumber;

var firstchar = document.getElementById('cardnumber').value.charAt(0);
if (firstchar == 3) {
    if (document.getElementById('visa').style.opacity) {
        document.getElementById('visa').style.opacity = .13;
        document.getElementById('mastercard').style.opacity = .13;
        document.getElementById('amex').style.opacity = 1;
        document.getElementById('discover').style.opacity = .13;
    }
    else {
        document.getElementById('visa').style.filter = 'alpha(opacity=13)';
        document.getElementById('mastercard').style.filter = 'alpha(opacity=13)';
        document.getElementById('amex').style.filter = 'alpha(opacity=100)';
        document.getElementById('discover').style.filter = 'alpha(opacity=13)';
    }
    document.getElementById('confirmCardType').innerHTML = 'American Express';
}
else if (firstchar == 4) {
    if (document.getElementById('visa').style.opacity) {
        document.getElementById('visa').style.opacity = 1;
        document.getElementById('mastercard').style.opacity = .13;
        document.getElementById('amex').style.opacity = .13;
        document.getElementById('discover').style.opacity = .13;
    }
    else {
        document.getElementById('visa').style.filter = 'alpha(opacity=100)';
        document.getElementById('mastercard').style.filter = 'alpha(opacity=13)';
        document.getElementById('amex').style.filter = 'alpha(opacity=13)';
        document.getElementById('discover').style.filter = 'alpha(opacity=13)';
    }
    document.getElementById('confirmCardType').innerHTML = 'Visa';
}
else if (firstchar == 5) {
    if (document.getElementById('visa').style.opacity) {
        document.getElementById('visa').style.opacity = .13;
        document.getElementById('mastercard').style.opacity = 1;
        document.getElementById('amex').style.opacity = .13;
        document.getElementById('discover').style.opacity = .13;
    }
    else {
        document.getElementById('visa').style.filter = 'alpha(opacity=13)';
        document.getElementById('mastercard').style.filter = 'alpha(opacity=100)';
        document.getElementById('amex').style.filter = 'alpha(opacity=13)';
        document.getElementById('discover').style.filter = 'alpha(opacity=13)';
    }
    document.getElementById('confirmCardType').innerHTML = 'MasterCard';
}
else if (firstchar == 6) {
    if (document.getElementById('visa').style.opacity) {
        document.getElementById('visa').style.opacity = .13;
        document.getElementById('mastercard').style.opacity = .13;
        document.getElementById('amex').style.opacity = .13;
        document.getElementById('discover').style.opacity = 1;
    }
    else {
        document.getElementById('visa').style.filter = 'alpha(opacity=13)';
        document.getElementById('mastercard').style.filter = 'alpha(opacity=13)';
        document.getElementById('amex').style.filter = 'alpha(opacity=13)';
        document.getElementById('discover').style.filter = 'alpha(opacity=100)';
    }
    document.getElementById('confirmCardType').innerHTML = 'Discover';
}
else {
    if (document.getElementById('visa').style.opacity) {
        document.getElementById('visa').style.opacity = .13;
        document.getElementById('mastercard').style.opacity = .13;
        document.getElementById('amex').style.opacity = .13;
        document.getElementById('discover').style.opacity = .13;
    }
    else {
        document.getElementById('visa').style.filter = 'alpha(opacity=13)';
        document.getElementById('mastercard').style.filter = 'alpha(opacity=13)';
        document.getElementById('amex').style.filter = 'alpha(opacity=13)';
        document.getElementById('discover').style.filter = 'alpha(opacity=13)';
    }
    document.getElementById('confirmCardType').innerHTML = '';
}
return true;

}

编辑:这是 jQuery 解决方案,它大大缩短了代码:

function setCardType() {
var cardnumber = document.getElementById('cardnumber').value;
cardnumber = cardnumber.replace(/[^0-9]/g,'');
document.getElementById('cardnumber').value = cardnumber;

var firstchar = cardnumber.charAt(0);
if (firstchar == 3) {
    $('#visa').css('opacity', .13);
    $('#mastercard').css('opacity', .13);
    $('#amex').css('opacity', 1);
    $('#discover').css('opacity', .13);
    document.getElementById('confirmCardType').innerHTML = 'American Express';
}
else if (firstchar == 4) {
    $('#visa').css('opacity', 1);
    $('#mastercard').css('opacity', .13);
    $('#amex').css('opacity', .13);
    $('#discover').css('opacity', .13);
    document.getElementById('confirmCardType').innerHTML = 'Visa';
}
else if (firstchar == 5) {
    $('#visa').css('opacity', .13);
    $('#mastercard').css('opacity', 1);
    $('#amex').css('opacity', .13);
    $('#discover').css('opacity', .13);
    document.getElementById('confirmCardType').innerHTML = 'MasterCard';
}
else if (firstchar == 6) {
    $('#visa').css('opacity', .13);
    $('#mastercard').css('opacity', .13);
    $('#amex').css('opacity', .13);
    $('#discover').css('opacity', 1);
    document.getElementById('confirmCardType').innerHTML = 'Discover';
}
else {
    $('#visa').css('opacity', .13);
    $('#mastercard').css('opacity', .13);
    $('#amex').css('opacity', .13);
    $('#discover').css('opacity', .13);
    document.getElementById('confirmCardType').innerHTML = '';
}
return true;

}

4

1 回答 1

3
$("#visa").css("opacity", .13);

jQuery 会自动为您处理交叉兼容性问题。太棒了...

于 2012-04-13T22:29:40.447 回答