- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我似乎无法使用innerHTML 在计算器下方显示答案。我以前曾使用警报框进行此操作,但理想情况下我想直接写入文档,以便可以包含链接和格式。
我成功地使用了下面的方法,并使用了一个不太复杂的脚本,该脚本不包含选择菜单(请参阅底部的工作代码)。除了显示区域的不同 ID 之外,这似乎是唯一的区别。
我通过 firebug 运行了这个,看起来所有的计算都在执行,并且脚本只有在显示结果时才会崩溃。
我对 javascript 还很陌生,而且我正在不断学习,因此您可以提供的任何详细信息都会非常有帮助。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript">
<!-- Begin CE MP Savings Calc script
function doMath3() {
var one = parseInt(document.theForm3.elements[0].value);
var two = parseInt(document.theForm3.elements[1].value);
var three = one * two;
var z4yearlyCash = three * 4.25 * 12;
var z3yearlyCash = three * 3.75 * 12;
var z2yearlyCash = three * 3 * 12;
var z1yearlyCash = three * 2.5 * 12;
var baseyearlyCash = three * 1.5 * 12;
var selection = document.getElementsByName("zone")[0].value;
var answerCEMP = document.getElementById("answerCEMP");
if (document.theForm3.elements[0].value == ""){
alert("Please enter the number of days a month you commute on Commuter Express.")
}
if (document.theForm3.elements[1].value == ""){
alert("Please enter the number of times you board a Commuter Express daily.")
}
if(one > 31){
alert("Come now, no month has " + one + " days! Please try again.");
}
else if(selection == "z4"){
var prodZ4 = z4yearlyCash - 1680;
if(prodZ4 > 0){
answerCemp.innerHTML="<div class='answerbox'><p>If you pay cash, your yearly expense is $" + z4yearlyCash + ".<br>Commuter Express Zone 4 Monthly Passes cost $1680 per year, <strong>a yearly savings of $" + prodZ4 + "<\/strong><br><strong><a href='http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=CTGY&Store_Code=LTSO&Category_Code=MP4'>Buy a Commuter Express Monthly Pass now.<\/a><\/strong><\/p><\/div>";
}
else if(prodZ4 <= 0){
answerCemp.innerHTML="<div class='answerbox'><p>Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. <strong><a href='#triptickets'>Try the Trip Tickets Calculator below.<\/a><\/strong><\/p><\/div>";
}
}
else if(selection == "z3"){
var prodZ3 = z3yearlyCash - 1488;
if(prodZ3 > 0){
alert("If you pay cash, your yearly expense is $" + z3yearlyCash + ". Commuter Express Zone 3 Monthly Passes cost $1488 per year, a yearly savings of $" + prodZ3 + ".");
}
else if(prodZ3 <= 0){
alert("Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. Try the Trip Tickets Calculator below.");
}
}
else if(selection == "z2"){
var prodZ2 = z2yearlyCash - 1200;
if(prodZ2 > 0){
alert("If you pay cash, your yearly expense is $" + z2yearlyCash + ". Commuter Express Zone 2 Monthly Passes cost $1200 per year, a yearly savings of $" + prodZ2 + ".");
}
else if(prodZ2 <= 0){
alert("Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. Try the Trip Tickets Calculator below.");
}
}
else if(selection == "z1"){
var prodZ1 = z1yearlyCash - 960;
if(prodZ1 > 0){
alert("If you pay cash, your yearly expense is $" + z1yearlyCash + ". Commuter Express Zone 1 Monthly Passes cost $960 per year, a yearly savings of $" + prodZ1 + ".");
}
else if(prodZ1 <= 0){
alert("Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. Try the Trip Tickets Calculator below.");
}
}
else if(selection == "Base"){
var prodBase = baseyearlyCash - 684;
if(prodBase > 0){
alert("If you pay cash, your yearly expense is $" + baseyearlyCash + ". Commuter Express Base Monthly Passes cost $684 per year, a yearly savings of $" + prodBase + ".");
}
else if(prodBase <= 0){
alert("Looks like you don't ride Commuter Express often enough to benefit from purchasing a Monthly Pass. Trip Tickets may be a better option for you. Try the Trip Tickets Calculator below.");
}
}
}
// End CE MP Savings Calc script -->
</script>
</head>
<body>
<div class="calcform">
<form name="theForm3">
<h2><a name="cemp" id="cemp"></a>You Do the Math: Commuter Express Monthly Pass Vs. Cash</h2>
<div class="formrow-calc">
<div class="calcform-col1">
<label for="trips-cemp">Daily boardings on Commuter Express Bus*:</label>
<input type="text" id="trips-cemp">
</div>
</div>
<div class="formrow-calc">
<div class="calcform-col1">
<label for="days-cemp">Days you ride Commuter Express <strong>per month</strong>:</label>
<input type="text" id="days-cemp"></div>
</div>
<div class="formrow-calc">
<div class="calcform-col1">
<label for="choosezone">Choose Zone:</label>
<select name="zone" id="choosezone">
<option value="Base">Base</option>
<option value="z1">Zone 1</option>
<option value="z2">Zone 2</option>
<option value="z3">Zone 3</option>
<option value="z4">Zone 4</option>
</select>
</div>
</div>
<div class="clear"></div>
<div align="center">
<button type="submit" onclick="doMath3(); return false;" class="btn-submit"><img src="img/btn_savings.png" alt="Show My Yearly Savings"></button>
</div>
</form>
<div id="answerCemp"></div>
</div>
</body>
</html>
这是正在运行的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript">
<!-- Begin DASH MP Savings Calc script
function doMath2() {
var one = eval(document.theForm.elements[0].value);
var two = eval(document.theForm.elements[1].value);
var three = one * two;
var yearlyCash = three * .5 * 12;
var prod = yearlyCash - 216;
var answer=document.getElementById("answer")
if (document.theForm.elements[0].value == ""){
alert("Please enter the number of days a month you ride DASH.");
}
if (document.theForm.elements[1].value == ""){
alert("Please enter the number of times you board a DASH bus daily.");
}
if (one > 31){
alert("Come now, no month has " + one + " days! Please try again.");
}
else {
if(prod > 0){
answer.innerHTML="<div class=answerbox><p>If you pay cash, your yearly expense is $" + yearlyCash + ".<br>DASH Monthly Passes cost $216 per year, <strong>a yearly savings of $" + prod + "<\/strong>.<br><strong><a href=http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=LADOTDMP1&Category_Code=MP4>Buy a DASH Monthly Pass now.<\/strong><\/a><\/p><\/div>";
}
else if(prod <= 0){
answer.innerHTML="<div class=answerbox><p>Looks like you don't ride DASH often enough to benefit from purchasing a Monthly Pass. However, you may benefit from the convenience of <strong><a href=http://store.ladottransit.com/Merchant2/merchant.mvc?Screen=PROD&Store_Code=LTSO&Product_Code=DTB1&Category_Code=DTB>DASH Ticket Books.<\/a><\/strong><\/p><\/div>";
}
}
}
// End DASH MP Savings Calc script -->
</script>
</head>
<body>
<div class="calcform">
<form name="theForm" action="#">
<h2>You Do the Math: DASH Monthly Pass Vs. Cash</h2>
<div class="formrow-calc">
<div class="calcform-col1">
<label for="trips-dash">Daily boardings on DASH bus*:</label>
<input type="text" name="trips" id="trips-dash">
</div>
</div><div class="calcform-col1">
<label for="days-dash">Number of days <strong>per month</strong> you ride DASH:</label>
<input type="text" name="days" id="days-dash">
</div>
<div class="clear"></div>
<div align="center">
<button type="submit" onclick="doMath2(); return false;" class="btn-submit">
<img src="img/btn_savings.png" alt="Show My Yearly Savings">
</button>
</div>
</form>
<div id="answer"></div>
</div>
</body>
</html>
最佳答案
在您的工作脚本中,您有 answer
而不是 answerCEMP
。你确实定义了它:
var answer = document.getElementById("answer");
您在失败的脚本中遗漏了该部分。此外,您在 div 上使用 name
而不是 id
。使用id
。更改您的 html:
<div id="answerCEMP"></div>
并在 JavaScript 中定义 answerCEMP
:
var answerCEMP = document.getElementById("answerCEMP");
关于Javascript insideHTML 不写入文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6146372/
在注册表中,我有 php 验证密码并将消息“good”发送回 div“password_feedback”。在if语句中,如果password_feedback ==“good”,则在“check”d
您好,我正在尝试更新以下脚本的innerHTML Your submission was successful. 我已经尝试过以下 var cls =document.getElementB
在我的脚本中 node1.data 和 node1.textContent 工作正常,但 node1.innerHTML 没有显示任何内容,我不明白为什么。 var text = []; functi
有谁知道如何从 IFRAME 中获取 HTML 我尝试了几种不同的方法: document.getElementById('iframe01').contentDocument.body.innerH
这是一个非常简单的程序,输出应该是 JavaScript,但我只得到 s。 document.getElementById("ma"
我有一个带有innerhtml函数的javascript代码,就像这样 var x = document.getElementById("gps"); function getLocation()
我正在使用 html 和 js,并且我想在用户单击某些内容时显示一段 html 代码。代码有点大,因此使用 document.getElementById("display").innerHTML =
我似乎无法使用innerHTML 在计算器下方显示答案。我以前曾使用警报框进行此操作,但理想情况下我想直接写入文档,以便可以包含链接和格式。 我成功地使用了下面的方法,并使用了一个不太复杂的脚本,该脚
我看到了很多关于这种模式的帖子,但仍然不明白为什么它在我的代码中不起作用。在 Ubuntu 10.04 上使用 Chromium。 function showDescription(){ va
我有一段代码,可以将 div 标签的内部 HTML 更改为状态消息,它包含在 try 方法中,唯一的问题是状态保持可见约 3 秒,然后消失。我认为这是由于 try 方法造成的,有什么办法可以阻止这个吗
我想避免每次想要更改时都必须在 5 个不同的页面上制作菜单。因此,我使用 Javascript 在运行时重新创建菜单,这样我只需更改该脚本即可立即在所有页面上更改菜单(我知道,天才吧?)。 但事情是这
我想从 API 中获取每个元素并将其显示在不同的类中。我在控制台中得到结果,但在页面上没有得到结果。请帮帮我。预先感谢您:) . 我已经将 JavaScript 与 Jquery 结合使用。 $.g
我对 JavaScript 还很陌生。我正在尝试在 的顶部添加一个 div 。但由于某种原因,它将我的代码添加为这样的文本 这是我目前的代码。 function addDark() { docu
function startMatch(deck, dealer, player){ var dealerX = document.querySelector('#player:first-c
我对innerHTML的理解并不完全清楚,所以我遇到了一些麻烦。 这会产生表外的所有 td。我做错了什么? var list = document.getElementById("procTable"
不确定这是否很奇怪或者只是我很愚蠢,但我在 JavaScript innerHTML 中遇到了问题。好的,我的 body 标签中有一个空的 DIV。我想要的是在其中写一个无序列表。所以,我做了这个:
我正在使用谷歌地图 API。当用户移动 map 上的标记时,提交按钮将从禁用状态更改为禁用状态。它将按钮从禁用状态更改为禁用状态,但我无法提交表单。什么也没发生。
我试图将我在文本输入中输入的所有内容大写,js 上的 caps 函数工作正常,我可以在控制台上看到,但 insidehtml 函数不会更改 html 文件 function clr() { doc
这个问题已经有答案了: Why does jQuery or a DOM method such as getElementById not find the element? (7 个回答) 已关闭
我正在为考试尝试一些东西,但无法使其发挥作用。这真的很简单,我不明白为什么不起作用。 Exercici 2 //rep 2 paràmetres d'entra
我是一名优秀的程序员,十分优秀!