Javascript元素为null

编程入门 行业动态 更新时间:2024-10-11 15:20:01
本文介绍了Javascript元素为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是javascrit的初学者,我试图获得表格结果全球

I am a begginer at javascrit, and im trying to get a form result globaly

单选按钮

<input type='radio' name='supporter' id='supporter' value='yes'>

和其他页面上的javascript

and the javascript on the nother page

<script type="text/javascript"> function show(){ var supporter = document.getElementById('supporter'); if (supporter.value == "yes") { alert("it works") } } show(); </script>

我一直收到错误,这个:支持者是空的

and i keep getting an error, this: supporter is null

可以请有人告诉我我缺少什么吗?

can please someone tell me what im missing?

推荐答案

这将取决于何时脚本被执行。 Javascript由浏览器在遇到时执行,这意味着如果您的代码片段存在于之前的 HTML元素中,那么在该时间点,HTML元素尚不存在。

It will depend on when the script is executed. Javascript is executed by the browser as it is encountered, which means that if your snippet of code exists in the page before the HTML element, then at that point in time, the HTML element does not yet exist.

一种解决方案是将Javascript放在页面的最后,或将该代码移到 document.onload b。处理器。

One solution would be to put the Javascript at the very end of the page, or move that code into the document.onload handler.

document.onload = function () { show(); };

更多推荐

Javascript元素为null

本文发布于:2023-11-30 06:07:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   Javascript   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!