简体   繁体   English

HTML标签中的条件格式

[英]Conditional Formatting in HTML Tags

Is there any option for using IF-ELSE conditioning in HTML tags 有没有在HTML标签中使用IF-ELSE调节的选项

 <if true>  do something   </if>  
 <else>     do something   </else>

There is, but it's really only used in IE to distinguish between different versions: 有,但它实际上只用于IE来区分不同的版本:

<!--[if IE6]>
    Things here!
<![endif]-->

Not to be pedantic, but HTML is a markup language, and isn't useful for conditional logic. 不是迂腐,但HTML是一种标记语言,对条件逻辑没用。

That being said, it sounds like what you're looking for is a bit of javascript. 话虽这么说,听起来你正在寻找的是一些javascript。 If you could add a bit more detail to your question, I could elaborate on how you could use javascript to do tasks with conditional logic. 如果您可以在问题中添加更多细节,我可以详细说明如何使用javascript来执行条件逻辑任务。

HTML was designed for document layout so the noscript and noframes are about as close as HTML gets to handling conditionals. HTML是为文档布局而设计的,因此noscript和noframe几乎与HTML处理条件一样接近。 You could conceivably approach this problem with javascript. 你可以想象用javascript来解决这个问题。

<div id='if-part' style='visibility: hidden;'>do something</div>
<div id='else-part' style='visibility: hidden'>do something</div>

<script>
    var node;
    if(true) {
        node = document.getElementById('if-part');
    }
    else {
        node = document.getElementById('else-part');
    }
    node.style.visibility = 'visible';
</script>

of course this only works if the client has javascript turned on. 当然这仅在客户端启用了javascript时才有效。

Conditional rendering of HTML is not a new concept, but it cannot be done using HTML exclusively. HTML的条件呈现不是一个新概念,但不能仅使用HTML来完成。 You would need to use either client side scripting or server side code to provide the conditional logic that would render your HTML accordingly. 您需要使用客户端脚本或服务器端代码来提供相应地呈现HTML的条件逻辑。

Have you guy's ever coded an email? 你有没有人编写过电子邮件? All of your java script is stripped by google. 你的所有java脚本都被google剥离了。 Furthermore, gmail on android does not support media queries, and different versions of outlook have their own quirks. 此外,Android上的gmail不支持媒体查询,不同版本的Outlook也有自己的怪癖。 You have no choice but to use conditional HTML if you want to emails that render well on a variety of email clients. 如果您想要在各种电子邮件客户端上呈现良好的电子邮件,您别无选择,只能使用条件HTML。

This is much like the second example: 这很像第二个例子:

<!--[if gte mso 9]>
    <style type="text/css">
    /* Your Outlook-specific CSS goes here. */
    </style>
<![endif]-->

However, if you are not going through an email client I would have to agree with everyone else and say you should use Java Script. 但是,如果您没有通过电子邮件客户端,我将不得不同意其他人并说您应该使用Java Script。

As has been said in other posts, HTML does not support conditional logic. 正如在其他帖子中所说,HTML不支持条件逻辑。 You have two choices here: 你有两个选择:

1) Generate the HTML dynamically using technologies such as PHP or XSLT 1)使用PHP或XSLT等技术动态生成HTML

2) Modify the HTML DOM after the fact using Javascript 2)使用Javascript后修改HTML DOM

With acknowledgement to Kevin Loney and his javaScript solution, we should also consider CSS conditionals. 在确认Kevin Loney及其javaScript解决方案后,我们还应该考虑CSS条件。 If the conditional is based on browser-sensing/responsive design, then javaScripting can be bypassed: 如果条件基于浏览器感知/响应式设计,则可以绕过javaScripting:

<div class='if-part'>show something</div>
<div class='else-part'>show something</div>

"Show something" in the sense that you would merely hide the condition which does not apply. 从某种意义上说“显示某种东西”,你只会隐藏不适用的条件。 In the example below, "if-part" is shown on mobile devices only. 在下面的示例中,“if-part”仅显示在移动设备上。 "else-part" is shown on larger screens. “else-part”显示在较大的屏幕上。

@media screen and (max-width:767px){
    .if-part{display:initial;}
    .else-part{display:none}
}
@media screen and (min-width:768px){
    .if-part{display:none;}
    .else-part{display:initial}
}

This answer offers even more CSS options to consider. 这个答案提供了更多的CSS选项供您考虑。

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

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