我有一个使用货币和价格属性定义的元素。将货币属性设置为英镑时,我想据此计算价格。我尝试钩住attrChanged方法,但是if条件永远不会记录为真
currencyChanged: function (attrName, oldVal, newVal) {
var actualPrice = this.getAttribute("usdprice");
actualPrice = parseInt(actualPrice);
var converter = 1;
if(newVal === "£") {
converter = 0.59;
console.log("true");
}
this.$.price.innerHTML = actualPrice*converter;
}
我使用以下命令更改货币
var a = document.querySelector("gmselect-element");
a.setAttribute("currency", "£");
当您使用已发布的属性时*更改后的观察者就像currencyChanged
参数是(oldVal,newVal)。只有在创建通用attributeChanged
生命周期方法(attrName,oldVal,newVal)时,才可以使用它。试试这个:
currencyChanged: function (oldVal, newVal) {
var actualPrice = this.getAttribute("usdprice");
actualPrice = parseInt(actualPrice);
var converter = 1;
if(newVal === "£") {
converter = 0.59;
console.log("true");
}
this.$.price.innerHTML = actualPrice*converter;
}
在此简化的jsbin上打开“开发工具”控制台,以查看其工作情况。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句