简体   繁体   English

根据其innerHTML显示/隐藏HTML元素

[英]Show/Hide HTML elements based on their innerHTML

I am using the following small piece of Javascript to make a basic calculation from two variable amounts and then display that value using innerHTML My javascript knowledge is limited so I had help putting this together. 我正在使用下面的一小段Javascript从两个可变量进行基本计算,然后使用innerHTML显示该值。我的JavaScript知识很有限,因此我有帮助将它们组合在一起。

<script>
 var f = "<?php echo $row_rsFormData['total_flats']; ?>";
 var c = "<?php echo $row_rsCount['count']; ?>";
 var r = Math.round(f / 2) - c
 document.getElementById("remaining").innerHTML = r;
</script>

I want to show different elements depending on the value of ' var r' 我想根据'var r'的值显示不同的元素

if var r = 0 如果var r = 0

I want to display this: 我想显示这个:

<p class="qualify">You have a sufficient amount of leaseholders to qualify for Right to Manage</p>

if var r > 0 如果var r> 0

I want to display this: 我想显示这个:

<p class="count">You need <span id="remaining"></span> more consenting leaseholders to qualify</p>

I realise this is simple for most.. so please be kind. 我知道这对大多数人来说都很简单。

Thanks in advance 提前致谢

I can't believe this hasn't been answered yet. 我不敢相信这还没有得到答案。 As the tags for the question are all client side, I have a client side solution: 由于问题的标签全部是客户端,因此我有一个客户端解决方案:

<script>
 var f = "<?php echo $row_rsFormData['total_flats']; ?>";
 var c = "<?php echo $row_rsCount['count']; ?>";
 var r = Math.round(f / 2) - c
 document.getElementById("remaining").innerHTML = r;
 if(r = 0)
 {
  $('#quality').show();
 }
 if(r > 0)
 {
  $('#count').show();
 }
</script>

Add an id attribute to both of your tags: 向两个标签添加一个id属性:

<p id="qualify" class="qualify">You have a sufficient amount of leaseholders to qualify for Right to Manage</p>

<p id="count" class="count">You need <span id="remaining"></span> more consenting leaseholders to qualify</p>

And initially hide them in the CSS: 最初将它们隐藏在CSS中:

#quality, #count
{
 display:none;
}

I hope this helps. 我希望这有帮助。

You can put id attribute in both of these elements and then based on value of r , either display/hide either of them. 您可以将id属性放在这两个元素中,然后根据r值显示或隐藏它们中的任何一个。

` `

<script>
    var f = "<?php echo $row_rsFormData['total_flats']; ?>";
    var c = "<?php echo $row_rsCount['count']; ?>";
    var r = Math.round(f / 2) - c
    document.getElementById("remaining").innerHTML = r;
    r = Number(r);
    if (r == 0) {
        document.getElementById('when0').style.display='block';
        document.getElementById('whenlarge0').style.display='none';
    } else if (r > 0) {
        document.getElementById('whenlarge0').style.display='block';
        document.getElementById('when0').style.display='none';
    }
</script>

` `

And your HTML becomes like this : 您的HTML变成这样:

<p id='whenlarge0' class="qualify">You have a sufficient amount of leaseholders to qualify for Right to Manage</p>

<p id='when0' class="count">You need <span id="remaining"></span> more consenting leaseholders to qualify</p>

Here I suppose that you don't have both paragraphs yet in your html page. 在这里,我想您在html页面中还没有两个段落。 Using the selector $('body') , you can append the paragraph according to the value of r . 使用选择器$('body') ,您可以根据r的值附加段落。

<script>
 var f = "<?php echo $row_rsFormData['total_flats']; ?>";
 var c = "<?php echo $row_rsCount['count']; ?>";
 var r = Math.round(f / 2) - c
 document.getElementById("remaining").innerHTML = r;
 if (r === 0) {
   $('body').append("<p class="qualify">You have a sufficient amount of leaseholders to qualify for Right to Manage</p>")
}
else if(r > 0){
   $('body').append("<p class="count">You need <span id="remaining"></span> more consenting leaseholders to qualify</p>")
}

</script>

You need to give the 您需要给

tags id and in your javascript code write an if statement to test your conditions, hide or show your 标签ID,并在您的javascript代码中编写if语句以测试您的条件,隐藏或显示您的

tags: 标签:

document.getElementById("your_id").style.display = "none"  // to hide
document.getElementById("your_id").style.display = "block" // to show

Here is my answer in simple js. 这是我在简单js中的答案。 Hope it will help you :-) 希望它能对您有所帮助:-)

You have your p tags already prepared and hidden 您已经准备好了p标签并将其隐藏

You will show the right one using style.display if it is set to "none" it will disappear, if it is set to "block" then it will appear. 您将使用style.display显示正确的一个,如果将其设置为“ none”,它将消失,如果将其设置为“ block”,则它将出现。 You get sort of pointers to the p tags that are already hidden at load time (toRemainder, toQualify ...) And then you show the right p tag according to the result of the calculation. 您将获得指向已在加载时隐藏的p标签的某种指针(toRemainder,toQualify ...),然后根据计算结果显示正确的p标签。 A mystery remains : What do you want to do when r < 0 ? 一个谜仍然存在:当r <0时,你想做什么? ;-) ;-)

Figures are random so you can test all the possible ouputs 数字是随机的,因此您可以测试所有可能的输出

 var f = Math.round(Math.random() * (60 - 50) + 50); var c = Math.round(Math.random() * (30 - 25) + 25); var r = Math.round(f / 2) - c var toRemainder = document.getElementById("remaining"); var toQualify = document.querySelector(".qualify"); var toCount = document.querySelector(".count"); var toSurprise = document.querySelector(".surprise"); if(r==0){ toQualify.style.display = "block"; } else if(r>0){ toRemainder.innerHTML = r; toCount.style.display = "block"; } else{ toSurprise.style.display = "block"; } 
 <p class="qualify" style="display:none;">You have a sufficient amount of leaseholders to qualify for Right to Manage</p> <p class="count" style="display:none;">You need <span id="remaining"></span> more consenting leaseholders to qualify</p> <p class="surprise" style="display:none;"> r < 0 : What do you want to do here ?</p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM