JavaScript无法正常工作,需要帮​​助 [英] JavaScript not working, need help

查看:55
本文介绍了JavaScript无法正常工作,需要帮​​助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一些JavaScript代码无法正常工作的问题。我确定我已经做了一些明显愚蠢的事情,但是我不能为我的生活做好准备

。有人可以帮忙吗?


这是XHTML文档(剥离相关部分):


<?xml version =" 1.0" encoding =" UTF-8"?>

<!DOCTYPE html PUBLIC" - // W3C // DTD XHTML 1.0 Strict // EN"

" ; http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

< html xmlns =" http://www.w3.org/ 1999 / XHTML" XML:朗= QUOT;恩" lang =" en">

< head>

< script type =" text / javascript" src =" switch.js">< / script>

< / head>

< body>

< div id =" one" style =" display:block;"> content< / div>

< div id =" two" style =" display:none;"> content< / div>

< div>< a href ="#"的onclick = QUOT;开关(); return false;"> Switch< / a>< / div>

< / body>

< / html>

这是JavaScript文件switch.js:


函数开关(){

document.getElementById(''one'') .style.display =''none'';

document.getElementById(''two'')。style.display =''block'';

}


你能看到问题吗?

解决方案

Manfred Kooistra于2007年1月24日写道comp.lang.javascript


我遇到一些JavaScript代码无法正常工作的问题。我确定我已经做了一些明显愚蠢的事情,但是我不能为我的生活做好准备

。有人可以帮忙吗?


这是XHTML文档(剥离相关部分):


<?xml version =" 1.0" encoding =" UTF-8"?>

<!DOCTYPE html PUBLIC" - // W3C // DTD XHTML 1.0 Strict // EN"

" ; http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

< html xmlns =" http://www.w3.org/ 1999 / XHTML" XML:朗= QUOT;恩" lang =" en">

< head>

< script type =" text / javascript" src =" switch.js">< / script>

< / head>

< body>

< div id =" one" style =" display:block;"> content< / div>

< div id =" two" style =" display:none;"> content< / div>

< div>< a href ="#"的onclick = QUOT;开关();返回

false;">切换< / a>< / div>

< / body>

< / html> ;


这是JavaScript文件switch.js:


函数开关(){

文件。 getElementById(''one'')。style.display =''none'';

document.getElementById(''two'')。style.display =''block'';
}


你能看到问题吗?



不要以单独的文件中的js开头,

你甚至不知道它是否已加载。

不要在usenet上使用标签。

请勿使用与你的Q无关的XML垃圾。

不要说不工作但是向我们展示错误代码和错误行号。


您/我们将看到,切换必须是保留字。


尝试:


函数mySwitch(){


- -

Evertjan。

荷兰。

(请在我的电子邮件地址中将x'变为点数)


谢谢,Evertjan。我将文件更改为:


< html>

< head>

< script language ="的javascript" type =" text / javascript">

//<![CDATA [

function mySwitch(){

document.getElementById (''one'')。style.display =''none'';

document.getElementById(''two'')。style.display =''block'';

}

//]]>

< / script>

< / head>

< body>

< div id =" one" style =" display:block;"> content< / div>

< div id =" two" style =" display:none;"> content< / div>

< div>< a href ="#"的onclick = QUOT;是myswitch(); return false;"> Switch< / a>< / div>

< body>

< / html>


没有错误信息,它什么都不做。


抱歉,有错误代码。


我将文件更改为HTML 4 Transitional with:


< script language =" javascript" type =" text / javascript">

<! -

函数mySwitch(){

document.getElementById(''一个'')。style.display =''none'';

document.getElementById(''two'')。style.display =''block'';

}

// - >

< / script>


,错误消息告诉我 ; mySwitch未定义并指向

我到第1行,其中包含:"< html>"。


有什么想法吗?


I have a problem with some JavaScript code not working. I''m sure I''ve
done something obviously stupid, but I can''t for the live of me figure
it out. Can someone please help?

This is the XHTML document (stripped to the relevant parts):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="switch.js"></script>
</head>
<body>
<div id="one" style="display: block;">content</div>
<div id="two" style="display: none;">content</div>
<div><a href="#" onclick="switch(); return false;">Switch</a></div>
</body>
</html>

and this is the JavaScript file switch.js:

function switch() {
document.getElementById(''one'').style.display = ''none'';
document.getElementById(''two'').style.display = ''block'';
}

Can you see the problem?

解决方案

Manfred Kooistra wrote on 24 jan 2007 in comp.lang.javascript:

I have a problem with some JavaScript code not working. I''m sure I''ve
done something obviously stupid, but I can''t for the live of me figure
it out. Can someone please help?

This is the XHTML document (stripped to the relevant parts):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="switch.js"></script>
</head>
<body>
<div id="one" style="display: block;">content</div>
<div id="two" style="display: none;">content</div>
<div><a href="#" onclick="switch(); return
false;">Switch</a></div>
</body>
</html>

and this is the JavaScript file switch.js:

function switch() {
document.getElementById(''one'').style.display = ''none'';
document.getElementById(''two'').style.display = ''block'';
}

Can you see the problem?

Do not start with a js in a separate file,
you do not even know if it is loaded.
Do not use tabs on usenet.
Do not the XML crap that is not partinent to your Q.
Do not say "not working" but show us the errorcode and errorline number.

And you/we will see, that "switch" must be a reserved word.

Try:

function mySwitch() {

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


Thanks, Evertjan. I changed the file to this:

<html>
<head>
<script language="javascript" type="text/javascript">
//<![CDATA[
function mySwitch() {
document.getElementById(''one'').style.display = ''none'';
document.getElementById(''two'').style.display = ''block'';
}
//]]>
</script>
</head>
<body>
<div id="one" style="display: block;">content</div>
<div id="two" style="display: none;">content</div>
<div><a href="#" onclick="mySwitch(); return false;">Switch</a></div>
<body>
</html>

There is no error message, it simply does nothing.


Sorry, there is an error code.

I changed the file to HTML 4 Transitional with:

<script language="javascript" type="text/javascript">
<!--
function mySwitch() {
document.getElementById(''one'').style.display = ''none'';
document.getElementById(''two'').style.display = ''block'';
}
// -->
</script>

and the error message tells me that "mySwitch is not defined" and point
me to line 1, which reads: "<html>".

Any ideas?


这篇关于JavaScript无法正常工作,需要帮​​助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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