嗨,我是新手,所以请原谅我非常基本的代码,我正在尝试打印生命路径计算的结果,但似乎无法检测到我想要得到的结果。
因此,我根据给定的示例更改了代码,这是Calculator()函数,在该函数中,我获取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;"/>
这是我以后才看到的。
最佳答案
所以,
如果FinalLifeNumber
是全局变量,则可以直接从switchVisible()
访问它。
否则,您可以像这样向switchVisible()
添加参数:
switchVisible(LifeNumber) {
// and use it like any other var as LifeNumber
}
调用它时,只需像这样调用
switchVisible()
:switchVisible(FinalLifeNumber);
关于javascript - onClick操作后打印功能结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54484277/