如何使用getElementByID引用ID传递的元素 [英] How to reference an element passed by ID to function using getElementByID

查看:86
本文介绍了如何使用getElementByID引用ID传递的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个函数中使用document.getElementByID,其中

ID作为参数传递。我在使用MS IE 6.0或Mozilla Firefox 0.9的commonCheck函数

内的同一语句中得到了相同的错误(元素没有

属性)。下面的评论表明

,如果我硬编码元素ID,我可以避免错误,但我显然想要参数化它。以下是我的代码的一些片段:


----- snippet 1 ----------------------- --------------------------------

< form method =" POST" action =" UserProfileMaint.htm"

onSubmit =" return validateTelnr(" txtPhone"," msgPhone",true);">

< p>& nbsp;< / p>

< p>& nbsp;< / p>

< table id =" tblMain"边界=" 0" width =" 100%">

< tr>

< td width =" 25%">& nbsp;< / td>

< td width =" 25%">电话< / td>

< td width =" 25%">< ; input type =" text"名称= QUOT; txtPhone" id =" txtPhone"

size =" 20">< / td>

< td width =" 25%">< input type =" hidden" name =" msgPhone"

id =" msgPhone">< / td>

< / tr>

- --- snippet 2 --------------------------------------------- ----------

函数validateTelnr(vfld,//要验证的元素

ifld,//要接收的元素的ID

info / error msg

reqd)//如果需要,则为true

{

var stat = commonCheck(vfld,ifld,reqd) );

-----片段3 --------------------------------- ----------------------

函数commonCheck(vfld,//要验证的元素

ifld ,//要接收的元素的ID

info / error msg

reqd)//如果需要,则为true

{

if(!document.getElementById)

返回true; //在此浏览器上不可用 - 将验证

留给服务器


var elem = document.getElementById(ifld); //这将在下一次失败

stmt

// var elem = document.getElementById(" txtPhone"); //评论:

这是有效的

if(!elem.firstChild)// ------> msg" elem没有

属性>

返回true; //在此浏览器上不可用

if(elem.firstChild.nodeType!= node_text)

返回true; // ifld是错误的节点类型


if(emptyString.test(vfld.value)){

if(reqd){

msg(ifld," error"," ERROR:required");

vfld.focus();

返回false;

}

I''m trying to use document.getElementByID inside a function where the
ID is passed as an argument. I get the same error ("Element has no
properties") on the same statement inside the commonCheck function
with either MS IE 6.0 or Mozilla Firefox 0.9. Comments below indicate
that if I hardcode an element ID I can avoid the error, but I
obviously want to parameterize it. Here are some snippets of my code:

-----snippet 1-------------------------------------------------------
<form method="POST" action="UserProfileMaint.htm"
onSubmit="return validateTelnr ("txtPhone", "msgPhone", true);">
<p>&nbsp;</p>
<p>&nbsp;</p>
<table id="tblMain" border="0" width="100%">
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">Phone</td>
<td width="25%"><input type="text" name="txtPhone" id="txtPhone"
size="20"></td>
<td width="25%"><input type="hidden" name="msgPhone"
id="msgPhone"></td>
</tr>
-----snippet 2-------------------------------------------------------
function validateTelnr (vfld, // element to be validated
ifld, // id of element to receive
info/error msg
reqd) // true if required
{
var stat = commonCheck (vfld, ifld, reqd);
-----snippet 3-------------------------------------------------------
function commonCheck (vfld, // element to be validated
ifld, // id of element to receive
info/error msg
reqd) // true if required
{
if (!document.getElementById)
return true; // not available on this browser - leave validation
to the server

var elem = document.getElementById(ifld); // this will fail on next
stmt
//var elem = document.getElementById("txtPhone"); //COMMENTED OUT:
this works
if (!elem.firstChild) //------> fails here with msg "elem has no
properties"
return true; // not available on this browser
if (elem.firstChild.nodeType != node_text)
return true; // ifld is wrong type of node

if (emptyString.test(vfld.value)) {
if (reqd) {
msg (ifld, "error", "ERROR: required");
vfld.focus();
return false;
}

推荐答案

JJA写道:
我正在尝试在一个函数里面使用document.getElementByID
[snip]< td width =" 25%">& nbsp;< / td>
< td width =" 25%">电话< / td>
< td width =" 25%">< input type =" text"名称= QUOT; txtPhone" id =" txtPhone"
size =" 20">< / td>
< td width =" 25%">< input type =" hidden" name =" msgPhone"
id =" msgPhone">< / td>
< / tr>
I''m trying to use document.getElementByID inside a function where the [snip] <td width="25%">&nbsp;</td>
<td width="25%">Phone</td>
<td width="25%"><input type="text" name="txtPhone" id="txtPhone"
size="20"></td>
<td width="25%"><input type="hidden" name="msgPhone"
id="msgPhone"></td>
</tr>



[snip]


请用空格替换标签(每个标签2个很好),并手动

包裹你的行约65个字符(允许几套

的回复引号)。


然后重新发布你的代码。我无法判断错误是我的尝试

来修复格式或编码。


干杯,Rob。


[snip]

Please replace tabs with spaces (2 per tab is nice), and manually
wrap your lines at about 65 characters (allows for a couple of sets
of reply quotes).

Then re-post your code. I can''t tell if the errors are my attempt
to fix the format or your coding.

Cheers, Rob.


JJA写道:


[snip]
JJA wrote:

[snip]
onSubmit =" return validateTelnr(" txtPhone"," msgPhone" ;,true);">
[snip] function validateTelnr(vfld,ifld,reqd)
[snip] var stat = commonCheck(vfld,ifld,reqd);
[snip] function commonCheck(vfld,ifld,reqd)
[snip] var elem = document.getElementById(ifld); //这将在下一次失败
// stmt
// var elem = document.getElementById(" txtPhone"); //评论出来:
//这是有效的
如果(!elem.firstChild)// ------>在这里失败了msg" elem没有
//属性"
onSubmit="return validateTelnr ("txtPhone", "msgPhone", true);"> [snip] function validateTelnr (vfld,ifld,reqd) [snip] var stat = commonCheck (vfld, ifld, reqd); [snip] function commonCheck (vfld,ifld,reqd) [snip] var elem = document.getElementById(ifld); // this will fail on next
// stmt
//var elem = document.getElementById("txtPhone"); //COMMENTED OUT:
//this works
if (!elem.firstChild) //------> fails here with msg "elem has no
// properties"




如果我重新格式化你的代码(很可能我没有),你是

推出txtPhone调用validateTelnr()时进入vfld。然后你再次使用相同的名字继续传递变量
每次



当你开始使用片段时3,你期待txtPhone在

变量ifld - 但你似乎一直把它放进vfld。


vfld(我认为)正在获取字符串msgPhone ,一个隐藏的输入字段

有一个* name *但没有* id *,所以没有id =" msgPhone"并且

调用什么都不返回 - 完全符合它。


一个简单的调试就是在你使用变量之前发出警报

在关键时刻找出它的价值,或者从失败中向前工作
或从顶部开始向前看看

中断的位置。在这种情况下,一行如下:


alert(''vfld is:''+ vfld

+''\ n ifld is:'' + ifld

+''\ n reqd是:''+ reqd);


会在开始时显示它是正确的...


希望有所帮助 - Rob。



If I have re-formatted your code right (chances are I haven''t), you are
putting "txtPhone" into vfld when you call validateTelnr(). You then
keep passing the variables on subsequent calls using the same names
each time.

When you get down to snippet 3, you are expecting "txtPhone" to be in
variable ifld - but you seem to have kept putting it into vfld.

vfld (I think) is getting the string "msgPhone", a hidden input field
that has a *name* but no *id*, so there is no id="msgPhone" and the
call returns nothing - exactly as it should.

A simple debug is to put an alert just before you use a variable to
find out what its value is at the critical moment, either working
backwards from the failure or forwards from the top to see where in
breaks. In this case, a line like:

alert('' vfld is: '' + vfld
+ ''\n ifld is: '' + ifld
+ ''\n reqd is: '' + reqd);

would have shown it was right at the start...

Hope that helps - Rob.


使用getElementById传递参数没有问题,试试这个:

< a id =" o22" href ="#"> asda< / a>< script>

function ig(id){

document.getElementById(id).style.visibility =''隐藏'';

}

ig(''o22'');

< / script>

一个有问题的问题是调用传递对象而不是字符串的函数:


< script>

ig(''o22'' ); //正确(传递一个字符串)

ig(o22); //错误(传递一个对象(变量))

< / script>

jo ***@cbmiweb.com (JJA)在留言中写道:< 2c ************************** @发布。谷歌。 com> ...
There is no problem of using getElementById whith passed arguments, try this:
<a id="o22" href="#">asda</a><script>
function ig(id){
document.getElementById(id).style.visibility = ''hidden'';
}
ig(''o22'');
</script>
A posible problem is calling the function passing an object instead of an string:

<script>
ig(''o22''); // Correct (passing an string)
ig(o22); // Bad (passing an object (variable))
</script>

jo***@cbmiweb.com (JJA) wrote in message news:<2c**************************@posting.google. com>...
我正在尝试在
ID作为参数传递的函数中使用document.getElementByID。我使用MS IE 6.0或Mozilla Firefox 0.9在commonCheck函数内的同一语句中得到相同的错误(Element has no
properties)。下面的评论表明,如果我硬编码元素ID,我可以避免错误,但我显然想要参数化它。以下是我的代码的一些片段:

-----片段1 --------------------------- ----------------------------
< form method =" POST" action =" UserProfileMaint.htm"
onSubmit =" return validateTelnr(" txtPhone"," msgPhone",true);">
< p>& nbsp;< / p>
< p>& nbsp;< / p>
< table id =" tblMain"边界=" 0" width =" 100%">
< tr>
< td width =" 25%">& nbsp;< / td>
< td width =" 25%">电话< / td>
< td width =" 25%">< input type =" text"名称= QUOT; txtPhone" id =" txtPhone"
size =" 20">< / td>
< td width =" 25%">< input type =" hidden" name =" msgPhone"
id =" msgPhone">< / td>
< / tr>
-----代码段2 ------- ------------------------------------------------ <无线电通信/> function validateTelnr(vfld,//要验证的元素
ifld,//要接收的元素的id
info / error msg
reqd)//如果需要,则为true
{
var stat = commonCheck(vfld,ifld,reqd);
-----片段3 ----------------------- --------------------------------
函数commonCheck(vfld,//要验证的元素
ifld,//要接收的元素的id
info / error msg
reqd)//如果需要,则为true
{
if(!document.getElementById)
返回真正; //在此浏览器上不可用 - 将验证留给服务器

var elem = document.getElementById(ifld); //这将在下一次失败
stmt
// var elem = document.getElementById(" txtPhone"); //评论出来:
这是有效的
if(!elem.firstChild)// ------>在这里失败,msgelem没有
属性
返回true; //在此浏览器上不可用
if(elem.firstChild.nodeType!= node_text)
返回true; // ifld是错误的节点类型

if(emptyString.test(vfld.value)){
if(reqd){
msg(ifld," error", ERROR:required);
vfld.focus();
返回false;
}
I''m trying to use document.getElementByID inside a function where the
ID is passed as an argument. I get the same error ("Element has no
properties") on the same statement inside the commonCheck function
with either MS IE 6.0 or Mozilla Firefox 0.9. Comments below indicate
that if I hardcode an element ID I can avoid the error, but I
obviously want to parameterize it. Here are some snippets of my code:

-----snippet 1-------------------------------------------------------
<form method="POST" action="UserProfileMaint.htm"
onSubmit="return validateTelnr ("txtPhone", "msgPhone", true);">
<p>&nbsp;</p>
<p>&nbsp;</p>
<table id="tblMain" border="0" width="100%">
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">Phone</td>
<td width="25%"><input type="text" name="txtPhone" id="txtPhone"
size="20"></td>
<td width="25%"><input type="hidden" name="msgPhone"
id="msgPhone"></td>
</tr>
-----snippet 2-------------------------------------------------------
function validateTelnr (vfld, // element to be validated
ifld, // id of element to receive
info/error msg
reqd) // true if required
{
var stat = commonCheck (vfld, ifld, reqd);
-----snippet 3-------------------------------------------------------
function commonCheck (vfld, // element to be validated
ifld, // id of element to receive
info/error msg
reqd) // true if required
{
if (!document.getElementById)
return true; // not available on this browser - leave validation
to the server

var elem = document.getElementById(ifld); // this will fail on next
stmt
//var elem = document.getElementById("txtPhone"); //COMMENTED OUT:
this works
if (!elem.firstChild) //------> fails here with msg "elem has no
properties"
return true; // not available on this browser
if (elem.firstChild.nodeType != node_text)
return true; // ifld is wrong type of node

if (emptyString.test(vfld.value)) {
if (reqd) {
msg (ifld, "error", "ERROR: required");
vfld.focus();
return false;
}



这篇关于如何使用getElementByID引用ID传递的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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