document.getElementById()未定义

时间:2013-12-19 18:16:43

标签: javascript html

我是论坛的新手,也是html的新手。我已经在论坛中尝试过可能的答案,但没有什么是缝合工作。虽然我说我是新手,但我已经尝试过浏览w3shool并且很难搞清楚这一点。

我想要的是当我点击按钮时,我希望它能看到<h1><h1>,你是= x,如果你是= x那么请改变工作。如果你=工作,请改为x。

<!DOCTYPE html>
<html>
<Head>
<title>Heather I Love You!</title>
</head>
<body style= background-color:#A8A8A8>


<!========This is the header===========>
<div id="header" style= "background-color:#00FF33;" >
<h1><p id="hheader">Heather this is a series of Pitures, Videos, and Poems to 

show you the love that I have for you!"</p></h1></div>

<!=============The side bar that will allow a list of where she Wants to 

go==========>

<div id="Menu" Style="background-

color:#660033;height:300px;width:125px;float:left;">
<Talbe>

<b><h3{color:white}>Categories</h3></b><br>
<button type="button" onclick="ChangeHead()">click here!</button><br>



<!--Functions-->
<Script>
function ChangeHead()
{
var g=document.getElementById("hheader")
  x="Heather this is a series of Pitures, Videos, and Poems to show you the 

love that I have for you!"
  work="Here are some pictures of us and the boys"
  if (document.getElementById("hheader").innertext == x)
    {
    document.getElementById("hheader").innerHTML= =work; 
    }
  else
    {
    document.getElementById("hheader").innerHTML== x
    }
}
</Script>
</body>

</Html>

2 个答案:

答案 0 :(得分:1)

假设您有一个带有ID&#39; hheader&#39;:

的元素
<div id='hheader'>Heather this is a series of Pitures, Videos, and Poems to show you the love that I have for you!</div>

以下脚本应该有效:

<script>
    function ChangeHead() {
  var g = document.getElementById("hheader");
  var x = "Heather this is a series of Pitures, Videos, and Poems to show you the love that I have for you!";
  var work = "Here are some pictures of us and the boys";

  if (document.getElementById("hheader").innerText == x) {
    document.getElementById("hheader").innerHTML = work; 
  } else {
    document.getElementById("hheader").innerHTML = x;
  }
};
ChangeHead();
</script>

这样脚本就会改变&#39; hheader&#39; div为work变量的值

我在您的代码中发现了一些语法错误,因此我对其进行了更正。

在这里你可以看到一个有效的演示: http://jsfiddle.net/robertp/LvcCe/

答案 1 :(得分:0)

1 - 您在第34行有一个未终止的字符串文字:

x="Heather this is a series of Pitures, Videos, and Poems to show you the 

love that I have for you!"

要使脚本正常工作,您必须在“to”和“show”之间用“\ n”替换字符串中的换行符。另外,请换掉换行符。

2 - innerText无法与Firefox一起使用,只需在第38行使用innerHTML:

if (document.getElementById("hheader").innertext == x)
如果要分配值,

3 - = =没有意义。为变量赋值时,仅使用1个等号。 只有在制作if语句时才使用两个!     观看:     第40行:

document.getElementById("hheader").innerHTML= =work;

和第44行:

document.getElementById("hheader").innerHTML== x

4 - 第13行在结尾处有一个额外的引号:

show you the love that I have for you!"

如果您替换所有这些错误,您的脚本将起作用。 请查看适用于此处的脚本的修改版本: http://lespointscom.com/a/misc/stackoverflow/2014_01_29/new.html

或者只是按原样复制粘贴此HTML:

<!DOCTYPE html>
<html>
<Head>
<title>Heather I Love You!</title>
</head>
<body style= background-color:#A8A8A8>


<!========This is the header===========>
<div id="header" style= "background-color:#00FF33;" >
<h1><p id="hheader">Heather this is a series of Pitures, Videos, and Poems to 

show you the love that I have for you!</p></h1></div>

<!=============The side bar that will allow a list of where she Wants to 

go==========>

<div id="Menu" Style="background-

color:#660033;height:300px;width:125px;float:left;">
<Talbe>

<b><h3{color:white}>Categories</h3></b><br>
<button type="button" onclick="ChangeHead()">click here!</button><br>



<!--Functions-->
<Script>
function ChangeHead()
{
var g=document.getElementById("hheader")
  x="Heather this is a series of Pitures, Videos, and Poems to \n\nshow you the love that I have for you!"
  work="Here are some pictures of us and the boys"
  if (document.getElementById("hheader").innerHTML == x)
    {
    document.getElementById("hheader").innerHTML=work; 
    }
  else
    {
    document.getElementById("hheader").innerHTML= x
    }
}
</Script>
</body>

</Html>