document.getElementById 问题?

riconyi 2003-10-15 11:09:53
谁能不能给出document.getElementById相关的语法及相关的例子,我看了一个例子用到这个,愣是看不懂!而我翻遍了手上所有的资料都找不到相关内容.
...全文
104 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
astra1 2003-10-15
  • 打赏
  • 举报
回复
……

各位老大都来了
slumcherry 2003-10-15
  • 打赏
  • 举报
回复
还要注意一点:
getElementById()只能工作于document对象,不想getElementsByTagName()能被文档中的任何元素调用.
孟子E章 2003-10-15
  • 打赏
  • 举报
回复
所有新版本的浏览器都支持的唯一的通用访问对象的方法:getElementById
建议用这个
astra1 2003-10-15
  • 打赏
  • 举报
回复
就好象slumcherry说的,老浏览器中的脚本引擎不支持document.getElementById这个方法

加了这个if就可以避免出错了
riconyi 2003-10-15
  • 打赏
  • 举报
回复
我E文很差,看到这些就头晕......
astra1 2003-10-15
  • 打赏
  • 举报
回复
if (document.getElementById)
{
....
}

意思是判断当前的脚本引擎是否支持getElementById这个方法
kingdomzhf 2003-10-15
  • 打赏
  • 举报
回复
getElementById Method

--------------------------------------------------------------------------------

Returns a reference to the first object with the specified value of the ID attribute.

Syntax

oElement = document.getElementById(sIDValue)
Parameters

sIDValue Required. String爐hat specifies the value of an ID attribute.

Return Value

Returns the first object with the same ID attribute as the specified value.

Remarks

If the ID value belongs to a collection, the getElementById method returns the first object in the collection.

Example

This example uses the getElementById method to return the first occurrence of the ID attribute value, oDiv.

<SCRIPT>
function fnGetId(){
// Returns the first DIV element in the collection.
var oVDiv=document.getElementById("oDiv");
}
</SCRIPT>
<DIV ID="oDiv">Div #1</DIV>
<DIV ID="oDiv">Div #2</DIV>
<DIV ID="oDiv">Div #3</DIV>
<INPUT TYPE="button" VALUE="Get Names" onclick="fnGetId()">
riconyi 2003-10-15
  • 打赏
  • 举报
回复
那在语句中这样用又是什么意思呢?如下:
if (document.getElementById)
{
....
}
slumcherry 2003-10-15
  • 打赏
  • 举报
回复
这个只能在IE5+/NN6中才能用.
document.getElementById(对象的id).属性|方法
astra1 2003-10-15
  • 打赏
  • 举报
回复
oElement = document.getElementById(sIDValue)

sIDValue:字符串参数,元素的ID名
具体用法如:

<span id=span1></span>
<script>
function window.onload()
{
var t1=document.getElementById("span1");//取得span1元素的引用
t1.innerText="ddddd";//这样就能控制了,效果相同语句是span1.innerText="ddddd";
}
</script>


明白?
--------------------------------------------------------

MSDN的资料:

getElementById Method

--------------------------------------------------------------------------------

Returns a reference to the first object with the specified value of the ID attribute.

Syntax

oElement = document.getElementById(sIDValue)
Parameters

sIDValue Required. String that specifies the value of an ID attribute.

Return Value

Returns the first object with the same ID attribute as the specified value.

Remarks

If the ID value belongs to a collection, the getElementById method returns the first object in the collection.

Example

This example uses the getElementById method to return the first occurrence of the ID attribute value, oDiv.

<SCRIPT>
function fnGetId(){
// Returns the first DIV element in the collection.
var oVDiv=document.getElementById("oDiv1");
}
</SCRIPT>
<DIV ID="oDiv1">Div #1</DIV>
<DIV ID="oDiv2">Div #2</DIV>
<DIV ID="oDiv3">Div #3</DIV>
<INPUT TYPE="button" VALUE="Get Names" onclick="fnGetId()">
Standards Information

This method is defined in World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧