简体   繁体   English

JavaScript中类似PHP的静态html完成?

[英]PHP-like static html completion in JavaScript?

is it possible to achive similiar results in JavaScript ? 是否有可能在JavaScript中获得类似的结果? My aim is to not write static html within string literals. 我的目标是不在字符串文字中编写静态html。

<?php
    if (someCondition) {
?>
I'm here, because PHP allows it.
<?php
    }
?>

For example: 例如:

<script>
    if (someCondition) {
</script>
I'm here, because JavaScript allows it.
<script>
    }
</script>

I am aware of the following solution: 我知道以下解决方案:

<p id = "variable">I'm here, because JavaScript allows it.</p>
<script>
    var p = document.getElementById('variable');
    if (!someCondition) {
        p.parentNode.removeChild(p);
    }
</script>

Nice thought, but it's not possible in the way you want it. 很好的想法,但它不可能以你想要的方式。

Every <script> element is evaluated as a JavaScript independent program, which will throw a syntax error in your case. 每个<script>元素都被评估为独立于JavaScript的程序,这会在您的情况下引发语法错误。

You'll have to generate the <p> element inside of your JavaScript. 您必须在JavaScript中生成<p>元素。

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

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