除alert和document.write之外打印输出的方法

时间:2015-06-15 19:23:32

标签: javascript html css

这是我的一些编码。我想直接在页面上打印语句,而不是使用alert或document.write。我尝试使用getElementId,但它对我来说效果不错,我在脚本的第一行尝试了它。其他人仍然是一个警报。帮助

<body>
<div id="myDiv1"></div>
<div id="myDiv2"></div>

Select one:<br>
<form name="locationform">
  <input type="checkbox" name="North">North <br>
  <input type="checkbox" name="South">South<br>
  <input type="checkbox" name="Equator">Equator<hr>
Select another one:<br>
  <input type="checkbox" name="Inward">Inward<br>
  <input type="checkbox" name="Outward">Outward<br><br>

  <input type="button" value="Submit" name="myButton"     onClick="myfunction()">

<button type="reset" value="Reset">Reset</button>
</form>

<script language="JavaScript">


function myfunction() {
  ***if((document.locationform.North.checked == true) &&     (document.locationform.Inward.checked == true)){
    document.getElementById('myDiv1').innerhtml = 'Some Test';***

  }else if((document.locationform.South.checked == true) && (document.locationform.Outward.checked == true)){
    alert("Case 4.");

}else if((document.locationform.Equator.checked == true) && (document.locationform.Outward.checked == true)){
    alert("Case 6.");

}else if((document.locationform.South.checked == true) && (document.locationform.Inward.checked == true)){
    alert("Case 3.");

}else if((document.locationform.North.checked == true) && (document.locationform.Outward.checked == true)){
    alert("Case 5.");

}else if((document.locationform.Equator.checked == true) && (document.locationform.Inward.checked == true)){
    alert("Case 2.");


} else if((document.locationform.Inward.checked == false) && (document.locationform.Outward.checked == false)){       
    alert("Select a valid entry");


 } else if((document.locationform.North.checked == true) && (document.locationform.South.checked == true)){       
    alert("Select a valid entry");

  } else if((document.locationform.North.checked == true) && (document.locationform.Equator.checked == true)){       
    alert("Select a valid entry");

} else if((document.locationform.Equator.checked == true) && (document.locationform.South.checked == true)){       
    alert("Select a valid entry");

}

</script>

3 个答案:

答案 0 :(得分:1)

您的代码中存在错误

document.getElementById('myDiv1').innerhtml = 'Some Test';

应该是

document.getElementById('myDiv1').innerHTML = 'Some Test';

在您的控制台中测试它。 element.innerhtml将返回undefined,因为这不是一种方法。

这是来源

http://www.w3schools.com/jsref/prop_html_innerhtml.asp

答案 1 :(得分:1)

HTML

<div id="write_here"></div>

JS:

document.getElementById('write_here').innerHTML = 'Print Text Here';

答案 2 :(得分:0)

innerHTML而非innerhtml

e.g。 document.getElementById('mainbar').innerHTML