这个代码好吗?

var wlocation = $(this).closest('.myclass').find('li a').attr('href');
if (wlocation.prop !== undefined) { window.location = wlocation; }

还是我应该做
var wlocation = $(this).closest('.myclass').find('li a').attr('href');
if (wlocation.prop !== "undefined") { window.location = wlocation; }

最佳答案

我喜欢这个:

if (wlocation !== undefined)

但是,如果您喜欢第二种方法,则不会像您发布的那样。这将是:
if (typeof wlocation  !== "undefined")

关于javascript - jQuery/Javascript代码检查(如果未定义),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13607712/

10-13 00:36