为什么这个js不能正常工作? [英] Why won't this js work properly?

查看:88
本文介绍了为什么这个js不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面,我第一次尝试使用哈希数组,如果''得分''== 0或

''未定义'',则以下脚本将失败并显示js-console消息:

''无法将undefined或null转换为对象

document.getElementById(target1).innerHTML = regOut;

decodeRegion(0,"目标")''


var region = new Array();

region [" 0"] =" a unspecified region";

region [" undefined"] ="未指定的区域" ;;

region [" 11"] =" the upper-outer quadrant" ;;




函数decodeRegion(得分,目标){

if(得分== 0 ||得分==''未定义'' ){return;} //必须有这个**

if(得分> 100){regOut1 =''和其他'';得分 - = 100;}

regOut = region [得分];

document.getElementById(target).innerHTML = regOut;

return;

}


**如果我不能逃脱,我会收到错误。


我'我已经尝试过if(得分== 0){regOut =''somestring;}之后

regOut = region [得分],并且还保证得分是一个字符串使用

得分.toString(10),但它仍然没有玩。


-

Ed Jay(删除M以通过电子邮件回复)

解决方案



Ed Jay写道:

function decodeRegion(score,target){
if(score == 0 || score ==''undefined''){return;} //必须有**


首先你是比较看分数是否等于_string_

''undefined''。您可能想要的是使用关键字undefined

代替。


得分==未定义

document.getElementById(target) .innerHTML = regOut;
从您收到的错误消息中,我会说您的问题在于此行上的
。并且从您的错误消息判断,您还没有想到检查目标是否为null或未定义。另外它

很高兴检查对象功能:


if(document.getElementById)

{

if(target)

{

document.getElementById(target).innerHTML = regOut;

}

}

返回;




这只是个人偏好,但在这种情况下我看不到需要

返回undefined。你的功能真的需要返回

undefined?或者一旦你返回undefined,你会在某个地方使用那个值

吗?


" web.dev" <我们******** @ gmail.com>写道:


Ed Jay写道:

function decodeRegion(score,target){
if(score == 0 ||)得分==''undefined''){return;} //必须有这个**
首先你要比较看看得分是否等于_string_
''undefined''。您可能想要的是使用关键字undefined


得分== undefined




确定。

document.getElementById(target).innerHTML = regOut;

从你收到的错误信息中,我会说你的问题就在这条线上


。并且从您的错误消息判断,您没有想过检查目标是否为null或未定义。另外它很好检查对象功能:




我正在使用< span id =" target">< /跨度>对于我所有的占位符。我已经检查了

(小心),他们都在那里。


顺便说一下......'目标是否有限制.length?''


另外,我用不同的分数和目标调用该函数十次

使用< body onLoad ="。 ...取代。是否可能存在时间问题?
if(document.getElementById)
{
if(target)
{
document.getElementById(target).innerHTML = regOut;
}
}

return;



这只是个人偏好,但在这种情况下我没有看到需要
返回undefined。你的功能真的需要返回
undefined?或者一旦你返回undefined,你会在某个地方使用那个值吗?




我自动将它用作逃脱。这没什么大不了的。


-

Ed Jay(删除M以通过电子邮件回复)


web.dev写道:

Ed Jay写道:

function decodeRegion(score,target){
if(score = = 0 ||得分==''未定义''){return;} //必须有这个**
首先你要比较看看得分是否等于_string_
''未定义'。您可能的意思是使用关键字undefined


得分== undefined




不,它更多可靠比较


typeof得分==未定义


测试效率更高


if(typeof score ==''undefined''|| score == 0)

{

return;

}


因为如果类型未定义,则无需检查值是否为
。更高效的是


if(!score)

{

return;

}


但这适用于任何虚假值。

document.getElementById(target).innerHTML = regOut;


从你收到的错误信息中,我会说你的问题在于


就在这一行。还要根据您的错误消息判断,您有




请了解如何发布。引用的文本应该清楚地区分

到你的文本。

没想到检查目标是否为null或未定义。另外它很好检查对象功能:

if(document.getElementById)


这是测试价值,而不是功能。


< URL:http://pointedears.de/scripts/test/whatami#inference>

{
if(target)< $
document.getElementById(target).innerHTML = regOut;




这是容易出错的废话。

这需要_minimum_不要破坏某些值是


函数isMethodType(s)

{

return(s ==" function" || s ==" object");

}


if(isMethodType(typeof document.getElementById) ))

{

var o;

if((o = document.getElementById(target)))

{

o.innerHTML = regOut;

}

}

PointedEars


In the following, my first attempt at using hash array, if ''score'' == 0 or
''undefined'' the following script fails with the js-console message:
''Could not convert undefined or null to object
document.getElementById(target1).innerHTML = regOut;
decodeRegion(0, "target")''

var region = new Array();
region["0"]="an unspecified region";
region["undefined"]="an unspecified region";
region["11"]="the upper-outer quadrant";
etc.

function decodeRegion(score,target) {
if (score == 0 || score == ''undefined'') {return;} // Must have this**
if (score > 100) {regOut1 = '' and other''; score -= 100;}
regOut = region[score] ;
document.getElementById(target).innerHTML = regOut;
return;
}

** If I don''t escape, I get the error.

I''ve tried things like if(score == 0) {regOut = ''somestring;} after
regOut=region[score] , and also guaranteeing that score is a string using
score.toString(10), but it still doesn''t play.

--
Ed Jay (remove M to respond by email)

解决方案


Ed Jay wrote:

function decodeRegion(score,target) {
if (score == 0 || score == ''undefined'') {return;} // Must have this**
First off you''re comparing to see if score is equal to a _string_
''undefined''. What you probably meant was to use the keyword undefined
instead.

score == undefined
document.getElementById(target).innerHTML = regOut; From the error message you received, I would say your problem lies right on this line. And also judging from your error message, you have
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:

if(document.getElementById)
{
if(target)
{
document.getElementById(target).innerHTML = regOut;
}
}
return;



This is just a personal preference, but in this case I don''t see a need
to return undefined. Does your function really need to return
undefined? Or once you return undefined, will you be using that value
somewhere?


"web.dev" <we********@gmail.com> wrote:


Ed Jay wrote:

function decodeRegion(score,target) {
if (score == 0 || score == ''undefined'') {return;} // Must have this**
First off you''re comparing to see if score is equal to a _string_
''undefined''. What you probably meant was to use the keyword undefined
instead.

score == undefined



OK.

document.getElementById(target).innerHTML = regOut;

From the error message you received, I would say your problem lies


right on this line. And also judging from your error message, you have
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:



I''m using <span id="target"></span> for all my placeholders. I''ve checked
(carefully) and they''re all there.

As an aside...is there a limit to ''target.length?''

Also, I''m calling the function ten times with different scores and targets
using <body onLoad="....>. Is it possible there''s a timing issue?
if(document.getElementById)
{
if(target)
{
document.getElementById(target).innerHTML = regOut;
}
}

return;



This is just a personal preference, but in this case I don''t see a need
to return undefined. Does your function really need to return
undefined? Or once you return undefined, will you be using that value
somewhere?



I automatically use it as an escape. It''s no big deal.

--
Ed Jay (remove M to respond by email)


web.dev wrote:

Ed Jay wrote:

function decodeRegion(score,target) {
if (score == 0 || score == ''undefined'') {return;} // Must have this**
First off you''re comparing to see if score is equal to a _string_
''undefined''. What you probably meant was to use the keyword undefined
instead.

score == undefined



No, it is far more reliable to compare

typeof score == "undefined"

And it is more efficient to test

if (typeof score == ''undefined'' || score == 0)
{
return;
}

because if the type is undefined, it is not necessary
to check for the value. Even more efficient would be

if (!score)
{
return;
}

but that would apply to any false-value.

document.getElementById(target).innerHTML = regOut;


From the error message you received, I would say your problem lies


right on this line. And also judging from your error message, you have



Please learn how to post. Quoted text should be clearly distinguishable
to your text.
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:

if(document.getElementById)
That is testing for value, not functionality.

<URL:http://pointedears.de/scripts/test/whatami#inference>
{
if(target)
{
document.getElementById(target).innerHTML = regOut;



And that is error-prone nonsense.

The _minimum_ required for this not to break with certain values is

function isMethodType(s)
{
return (s == "function" || s == "object");
}

if (isMethodType(typeof document.getElementById))
{
var o;
if ((o = document.getElementById(target)))
{
o.innerHTML = regOut;
}
}
PointedEars


这篇关于为什么这个js不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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