嗨,我是一个新手,所以请原谅我很基本的代码,我想打印的人生轨迹计算的结果,但它似乎并没有检测到我想要得到的结果。
因此,我根据给定,这里的样品,其中我得到FinalLifeNumber的值,并把它传递给switchVisible()计算器()函数改变了代码。
document.getElementById("PathNumber").innerHTML = "Life Path Number: " + sumMonth + sumDays + FinalYear + " = " + FinalLifeNumber;
switchVisible(FinalLifeNumber);
}
我试图传递给该下一个功能switchVisible(),其应该输出它等于到div类。
function switchVisible(FinalLifeNumber) {
var cal = FinalLifeNumber;
document.getElementsByClassName('LP1')[0].style.display = "none";
document.getElementsByClassName('LP2')[0].style.display = "none";
document.getElementsByClassName('LP3')[0].style.display = "none";
document.getElementsByClassName('LP4')[0].style.display = "none";
document.getElementsByClassName('LP5')[0].style.display = "none";
document.getElementsByClassName('LP6')[0].style.display = "none";
document.getElementsByClassName('LP7')[0].style.display = "none";
document.getElementsByClassName('LP8')[0].style.display = "none";
document.getElementsByClassName('LP9')[0].style.display = "none";
switch (cal) {
case 1: document.getElementsByClassName('LP1')[0].style.display = "block"; break;
case 2: document.getElementsByClassName('LP2')[0].style.display = "block"; break;
case 3: document.getElementsByClassName('LP3')[0].style.display = "block"; break;
case 4: document.getElementsByClassName('LP4')[0].style.display = "block"; break;
case 5: document.getElementsByClassName('LP5')[0].style.display = "block"; break;
case 6: document.getElementsByClassName('LP6')[0].style.display = "block"; break;
case 7: document.getElementsByClassName('LP7')[0].style.display = "block"; break;
case 8: document.getElementsByClassName('LP8')[0].style.display = "block"; break;
case 9: document.getElementsByClassName('LP9')[0].style.display = "block"; break;
}
}
这是示例div类。
<div class="LP1">
<p id="LifePlan1">
<h1 style="background-color:brown;"><b> The Natural Born Leader </b></h1>
The Life Path 1 is one of leadership and trailblazing. With a strong sense of independence, you do not like relying on other people, especially if you feel they are holding you back. Often, you may feel like it is better to go it alone.
<br />
You would do well as an entrepreneur because you aren't afraid to take risks! You march to the beat of your own tune and the people around you generally don't hear until the tune is completed and on the top 100 charts. You tend to do things your own way and create innovation by stirring up the pot. People such as Henry Ford, Charlie Chaplin, Ozzy Osbourne, Tom Cruise, all stayed true to their Life Path 1, you should too.
<br />
As a Life Path 1, be careful and don't try to control everything and everyone around you as you plow headlong towards your goals. In life it is still beneficial to maintain a balance.
<br />
Confidence, creativity, and originality are very popular characteristics of a Life Path 1.
</p>
</div>
这是显示它的按钮。
<input type="submit" value="calculate" onclick="calculator(); switchVisible(FinalLifeNumber);" style="background-color:cornflowerblue; object-position:100px;"/>
这是我以后看到的。
答案 0 :(得分:1)
所以
如果FinalLifeNumber
是全局变量,则可以直接从switchVisible()
访问它。
否则,可以将参数添加到switchVisible()
是这样的:
switchVisible(LifeNumber) {
// and use it like any other var as LifeNumber
}
呼叫时,只需像这样呼叫switchVisible()
:
switchVisible(FinalLifeNumber);
答案 1 :(得分:1)
我也是新手,但我建议做些不同。使数组lifeplan[1]
到lifeplan[9]
,并使用每个数字要显示的文本(包括html标签,例如h1 style = ...等)。然后在html中仅创建一个空的div元素id="lifeplan"
,而不是隐藏其中的9个。然后在主函数中计算出从1到9的FinalLifeNumber后,document.getElementById("lifeplan").innerhtml=lifeplan[FinalLifeNumber]
如果您不喜欢它,请在函数FinalLifeNumber()最后添加return result
...您的号码在哪里...第二种方法是在脚本外部以{{1}开头的函数中声明变量FinalLifeNumber },那么它将是全局的,并且对以下所有功能可见。