JavaScript代码帮助

时间:2016-05-10 05:19:31

标签: javascript html

我编写了这几行代码,并希望一旦按下按钮就可以更改文本,但它不起作用。你能找出问题吗?



var omari = "Omari Lamar";
function omari (){
    el = document.getElementById('slice');
    el.textContent = omari + "Is a computer Programmer!";
}

<html>
    <head>
        <title></title>
    </head>
    <body>
       <h1>Title Example</h1>
       <button onclick="omari();">Click me</button>
       <div id="slice">
           sample text
       </div>
       <script src="app.js"></script>
   </body>
</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

将代码更改为:

var omariName ="Omari Lamar";
function omari (){
    el = document.getElementById('slice');
    el.textContent = omariName + "Is a computer Programmer!";

}

答案 1 :(得分:0)

在这里,您的函数名称和变量名称是相同的

&#13;
&#13;
<html>	
	<head>
		<title>

		</title>
		<script src="app.js"></script>
	</head>
	<body>
		<h1>Title Example</h1>

		<button onclick="omari();">Click me</button>
		<div id="slice">
		sample text
		</div>

		<script src="app.js"></script>
	</body>
</html>
&#13;
{{1}}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这是问题所在: 你有一个名为omari的变量和一个函数名omari。所以,当你尝试调用函数omari时,javascript实际上是查看变量定义,而不是函数。只是给他们不同的名字。

尝试以下代码。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test</title>
</head>

<body>
<script>

    var _omari ="Omari Lamar";
    function omari(){
        el = document.getElementById('slice');
        el.innerHTML = _omari + " Is a computer Programmer!";

    }
</script>

            <h1>Title Example</h1>

            <button onclick="omari();">Click me</button>
            <div id="slice">
            sample text
            </div>

</body>
</html>
相关问题