我需要测试javascript对象属性是否存在。
例如,查看jquery扩展是否可用。下一行返回浏览器控制台中未定义的内容:
$("#myid").toc
我想在transcrypt中测试这个值,我尝试了:
getattr(S ("#myid")、"toc")为空
但这不起作用
发布于 2018-02-14 19:31:52
下列措施将起作用:
HTML:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<div id="myid"></div>
<script src="__javascript__/test.js"></script>
</body>
</html>跨地窖:
__pragma__ ('alias', 'S', '$')
print (111, getattr (S ('#myid') [0], 'style') != None)
print (222, getattr (S ('#myid') [0], 'toc') != None)
console.dir (S ('#myid') [0])它打印:
111 True
222 False
div#myid作为一种选择,您可以始终在Transcrypt中嵌入JS:
__pragma__ ('js', '{}', '''
console.log (333, $("#myid")[0].style != undefined);
console.log (444, $("#myid")[0].toc != undefined);
''')其中的指纹:
333 true
444 false顺便说一下,我更喜欢Python语法。但是在边缘情况下,JS总是作为一种逃避而存在,尽管您可能永远不需要它。
https://stackoverflow.com/questions/48794194
复制相似问题