开发者

Calling typeof operator in jQuery Template data tags?

Is it possible to call typeof(x) within a data rendering template tag?

${typeof(x)}

Obligatory example jsFiddle

Workaround

I can alias the typeof operator as a custom template function and it works fine, but I would like to avoid that extra level of redirec开发者_如何学Gotion if I can.

// calling JavaScript
$("#reflectTemplate").tmpl(data, {
    getType: function(itemToCheck) {
        return typeof(itemToCheck);
    }
}).appendTo(".results");

<!--template tag-->
${$item.getType(prop)}


What you want is to force the result to be a string. One or both of these should work in most browsers:

${String(typeof(prop))}
${typeof(prop)+''}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜