onclick是Null?

时间:2013-02-05 04:06:57

标签: javascript html

我不知道我的代码是否有效,因为我无法通过onlclick为null。我正在尝试从textarea中获取文本并将其放入HTML中的段落中。带一个复选框。如果选中此框,则会清除<p>标记。如果未选中,它会将内容保留在p-tag中,只需创建一个新的p-tag。谢谢你们

window.onload = function()  { 
document.getElementById("paste").onclick = function() {


//var y = document.getElementById('paste').onclick
var paragraph = document.getElementById('box').value;
var x = document.getElementById("write");
var getInfo = document.getElementById('ParaWrite');

 if (x.checked === true)
     paragraph =+ "<p>"+paragraph+"</P>";
 else
 return;
getInfo.innterHTML === paragraph;

  }    
}


HTML 
<!doctype html>
<html>
<head>
<meta charset="utf-8">

<title>TeXt BoX</title>


<link rel="stylesheet" href="../css/normalize.css">
<link rel="stylesheet" href="Textbox.css">


</head>

<body>
<h1></h1>
<form name="form1" id="form1">

<input type='checkbox' name='write' id='write' value='Check' />

<label for='write'>Append Paragraph</label><br/>

<textarea type="text" id="box"/></textarea>

<input  type='button' value="Paste typed text" id="Paste"/>

</form>
<p id="ParaWrite"></p>


<script type="text/javascript" src="TextPara.js"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

id name区分大小写。将您的onclick分配呼叫更改为

document.getElementById("Paste").onclick = 

paste更改为Paste

同样不是=+正确的是+=

答案 1 :(得分:1)

您的选择器正在寻找ID paste,但您的元素的ID为Paste

var thisWillBeNull = getElementById('paste') ==> null

thisWillBeNull.onclick ==> TypeError: Cannot read property 'onclick' of null