简体   繁体   中英

How to embed a JavaScript if clause into a HTML code

I want to embed a JavaScript if clause into a HTML code.There are two buttons named success and danger those are separated by a if clause. I tried for it as following manner.

<script>
    if (1 < 18) {
</script>
       <button type="button" class="btn btn-success btn-rounded">Success</button>
<script>
    }
    else{
</script>
       <button type="button" class="btn btn-danger btn-rounded">Danger</button>
<script>
    }
</script>

But it did not work in properly. Then what should I do for it.

There's a few different ways you can do this in Angular. The simplest change would be to just use *ngIf :

<button *ngIf="1 < 18" type="button" class="btn btn-success btn-rounded">Success</button>
<button *ngIf="1 >= 18" type="button" class="btn btn-danger btn-rounded">Danger</button>

You'll need to write the HTML code with the JavaScript, see below.

<script type="text/javascript">
var i = 0;
window.onload = function(){
if (i == 0)
{
     document.getElementById('somearea').innerHTML = '<input type="button" value="Some Value 1"/>';
}
else
{
     document.getElementById('somearea').innerHTML = '<input type="button" value="Some Value 2"/>';
}
}
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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