我有一个页面,其中包含以下html,其中包含不同的电话号码:
<div class="crm-content crm-contact_phone primary">
<span>5555551212</span>
</div>
电话号码本身使用{$ phone.i.phone}形式的smarty变量显示,其中i是电话号码数组中的数组键 .
我希望能够使用js更改这些电话号码的格式 .
因此,对于一个电话号码,我在我的smarty .tpl文件中使用以下内容:
{literal}
cj(function($){
var phoneNumber = {/literal}{$phone.1.phone}{literal};
var phoneNumberFormatted = '(' + phoneNumber.substr(0,3) + ') ' + phoneNumber.substr(3,3) + '-' + phoneNumber.substr(6);
$(".crm-contact_phone span").text(phoneNumberFormatted);
});
{/literal}
所以我想,我需要做一些事情:
$('.crm-contact_phone span').each(function(i, obj) {
var phoneNumber = '' + {/literal}{$phone.1.phone}{literal};
}
但我不知道如何用javascript索引i替换smarty变量中的1 .
有任何想法吗?谢谢 .
2 回答
我最后采用了不同的方法,使用以下方法将整个smarty数组保存到js数组:
然后我可以使用纯js访问电话号码:
试试这个
使用 bracket Notation 并将其替换为 i .