最简单的getElementById问题。 [英] Simplest getElementById question ever.

查看:62
本文介绍了最简单的getElementById问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不起作用?


<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"

http://www.w3.org/TR/html4/loose.dtd">

< html>

< head>

< meta http-equiv =" Content-Type" content =" text / html;

charset = iso-8859-1">

< script language =" javascript">

alert(document.getElementById(''pst''));

< / script>

< / head>

< body>

< div id =" subject">

< form action =""方法= QUOT;获得"大于1。你想

< div id =" pst">

< a href ="#">写一个支持意见< / a> ;或者< a href ="#">写一个

不同意见< / a>,或者< a href ="#">写一般评论< / a>

< / div>

< p id =" subject_list">& nbsp;< / p>

< / form>

< / div>

< / body>

< / html>


它杀了我。有什么明显的东西我不见了吗?谢谢。

Why isn''t this working?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="javascript">
alert(document.getElementById(''pst''));
</script>
</head>

<body>
<div id="subjects">
<form action="" method="get">1. Would you like to
<div id="pst">
<a href="#">write a supporting opinion</a>, or <a href="#">write a
differing opinion</a>, or <a href="#">write a general comment</a>
</div>
<p id="subject_list">&nbsp;</p>
</form>
</div>
</body>
</html>

It''s killing me. Is there something obvious I''m missing? Thanks.

推荐答案

文件必须先加载才能引用document.getElementById;


将它包装在一个函数中并调用它onload:


< script language =" javascript">

函数saywhat(){

alert(document.getElementById(''pst''));

}

onload = sayWhat;

< ; / script>
The document has to load before you can reference document.getElementById;

wrap it in a function and call it onload:

<script language="javascript">
function saywhat(){
alert(document.getElementById(''pst''));
}
onload=sayWhat;
</script>





Rich Hephner写道:


Rich Hephner wrote:
为什么不是'这有用吗?

<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"
" http://www.w3.org /TR/html4/loose.dtd">
< html>
< head>
< meta http-equiv =" Content-Type" content =" text / html;
charset = iso-8859-1">
< script language =" javascript">
alert(document.getElementById(''pst ''));
< / script>
Why isn''t this working?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="javascript">
alert(document.getElementById(''pst''));
</script>




浏览器解析文档并在解析时执行脚本块

it所以在此时调用document.getElementById(''pst'')它

返回null,因为找不到具有该id属性值的元素。所以如果你看到一个带有null的警告对话框,那么

的例子会按预期工作。

也许你想使用

window.onload = function( evt){

alert(document.getElementById(''pst''));

};


-


Martin Honnen
http:// JavaScript。 FAQTs.com/


Rich Hephner在16 mei 2005上写了comp.lang.javascript
Rich Hephner wrote on 16 mei 2005 in comp.lang.javascript:
为什么这不起作用?

<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"
" http://www.w3.org/TR/html4/loose.dtd">
< html>
< head>
< meta http-equiv ="内容型" content =" text / html;
charset = iso-8859-1">
< script language =" javascript">
alert(document.getElementById(''pst ''));
< / script>
< / head>

< body>
< div id =" subject">
< form action =""方法= QUOT;获得"大于1。你想
< div id =" pst">
< a href ="#">写一个支持意见< / a>,或者< a href = "#">写一个
不同意见< / a>,或< a href ="#">写一般性评论< / a>
< / div>
< p id =" subject_list">& nbsp;< / p>
< / form>
< / div>
< / body> ;
< / html>

它杀了我。有什么明显的东西我不见了吗?谢谢。
Why isn''t this working?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="javascript">
alert(document.getElementById(''pst''));
</script>
</head>

<body>
<div id="subjects">
<form action="" method="get">1. Would you like to
<div id="pst">
<a href="#">write a supporting opinion</a>, or <a href="#">write a
differing opinion</a>, or <a href="#">write a general comment</a>
</div>
<p id="subject_list">&nbsp;</p>
</form>
</div>
</body>
</html>

It''s killing me. Is there something obvious I''m missing? Thanks.




1执行警报时元素尚不存在。


2你可能不喜欢我想要显示元素,但是该元素的一些可写属性

,如tagNeme,一些样式元素或innerHTML。

IE将返回[object]但是其他浏览器可以有其他想法。


-

Evertjan。

荷兰。

(在我的电子邮件地址中用点替换所有十字架)



1 the element does not yet exist at the time your alert is executed.

2 you probably don''t want to show the element, but some writable property
of that element, like the tagNeme, some style element or the innerHTML.
IE would return [object] but other browsers could have other ideas.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)


这篇关于最简单的getElementById问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆