Window.onload和订购

用户3893480

在下面的代码中,我似乎无法理解事件的顺序。当我在浏览器中加载它时,在第一个div呈现之前,我会收到警报,并且.onclick事件侦听器找不到要附加到的translate_button元素。我认为由于使用window.onload,我的脚本将在上述html加载后执行。这与html中的内联javascript有关吗?

<div id='MicrosoftTranslatorWidget' class='Dark' id='translate_button'></div><script type='text/javascript'>setTimeout(function(){{var s=document.createElement('script');s.type='text/javascript';s.charset='UTF-8';s.src=((location && location.href && location.href.indexOf('https') == 0)?'https://ssl.microsofttranslator.com':'http://www.microsofttranslator.com')+'/ajax/v3/WidgetV3.ashx?siteData=ueOIGRSKkd965FeEGM5JtQ**&ctf=True&ui=true&settings=Manual&languages=es,pt,fr,it,tr,zh-CHS,zh-CHT,ru,de,en';var p=document.getElementsByTagName('head')[0]||document.documentElement;p.insertBefore(s,p.firstChild); }},0);</script>
<div class='disclaimer-link' id='disclaimer-link' style='display:none'>Disclaimer</div>

<script type="text/javascript">

window.onload = function () {
    var button = document.getElementById("translate_button");
    button.onclick = alert("lol");
};
</script>
尤里·加兰特(Yuriy Galanter)

您正在分配函数执行。如果您想分配实际功能,则需要类似

button.onclick = function() { alert("lol"); }

另一件事-您为DIV定义了两次ID。删除其中一个,然后使用剩余的一个。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章