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

标签 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>

最佳答案

你的代码有问题

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

应该是

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

在您的控制台中对其进行测试。 element.innerhtml 将返回 undefined 因为那不是一个方法。

这是来源

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

关于javascript - 除了 alert 和 document.write 之外打印输出的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30853367/

相关文章:

javascript - 如何用标准 json 制作 jquery Datatable 数组?

html - 使用不同的浏览器问题时使用不同的 CSS?

html - CSS 计算宽度和高度值

javascript - 带有 ScrollMagic 引脚的网站

javascript - 移动设备的特定 css 但不基于屏幕分辨率

javascript - 在 js 中维护元素组之间关系的最佳实践

javascript - 在拖动一个 div 的同时,将另一个 div 与被拖动的 div 一起移动

javascript - 我有工作的 JavaScript,但是当我更改服务器并链接时它不起作用

html - 在一个元素中使用多个供应商标签是一种不好的做法吗?

PHP:允许用户更改背景颜色不起作用?