简体   繁体   English

复制HTML,CSS中的设计模式?

[英]Replicating design patterns in HTML, CSS?

Suppose you have this following code in HTML - 假设您在HTML中具有以下代码-
<div id="pattern">
<span class="a">Content</span>
</div>

And suppose you wanted to replicate and show this pattern at different positions on the page. 并假设您想在页面上的不同位置复制并显示此模式。 How would you do it? 你会怎么做?
Each of these pattern's will have a specific styling though. 这些模式中的每一个都会有特定的样式。

It is very easy to do using jQuery. 使用jQuery非常容易。

$('#pattern')
    .clone()
    .addClass('variate-styling')
    .appendTo('#container');

This piece of code will replicate pattern block, add some new styling effect and put it into another position on your page. 这段代码将复制模式块,添加一些新的样式效果并将其放置在页面上的其他位置。

really vague, but lets see. 真的很模糊,但让我们看看。

Content 内容

this repeats. 重复一遍。 so add a class of patterns to div, i'll leave that id there for you, but we're not using it @ the moment. 因此,将一类模式添加到div中,我将为您保留该ID,但此刻我们不会使用它。

Content 内容

so the repeating sections are set. 这样就设置了重复部分。 set default/generic styling for all here: .patterns{} .patterns a{} 在此处设置所有样式的默认/通用样式:.patterns {} .patterns a {}

you want different positions? 您想要不同的职位吗? ok, say you have one in your #column, #main-content and #footer id's. 好的,假设您在#列,#main-content和#footer ID中有一个。 you can style these 2 easy ways: 1) use parent in cascade to target specific repeating section: 您可以通过以下两种简单的方式设置样式:1)在层叠中使用父级定位特定的重复部分:

Content Content Content 内容内容内容

so to specify individual styles here, after default styles: .patterns{background-color:#c0c0c0} .patterns a{} 因此可以在默认样式之后在此处指定各个样式:.patterns {background-color:#c0c0c0} .patterns a {}

/* declare unique styles */ / *声明唯一样式* /

column .patterns{background-color:#000} 列.patterns {background-color:#000}

main-content .patterns{background-image:url("deez.png") 0 0 no-repeat} 主要内容.patterns {background-image:url(“ deez.png”)0 0不重复}

footer .patterns{background-color:#yourM0m} 页脚.patterns {background-color:#yourM0m}

OR you could ditch the ids and give each .pattern its own id, but the most versatile, optimized answer is to ditch the ids and use classes only, so each .patterns that needed an alternate style would also have to have said class added to it. 或者,您可以抛弃ID并为每个.pattern分配自己的ID,但是最灵活,最优化的答案是抛弃ID并仅使用类,因此,每个需要替代样式的.pattern都必须将class添加到它。

if i understood your question correctly, this is an extremely more optimized method than relying on a programming language (aside from JavaScript) to do it for you. 如果我正确理解了您的问题,这是比依靠编程语言(除了JavaScript)为您完成的方法更加优化的方法。 And while JavaScript is the optimal language to use with the Browser, aka Front-End....you do not need it to achieve what you are seeking. 尽管JavaScript是与浏览器一起使用的最佳语言,但也称为前端。...您不需要它即可实现所需的功能。 hope this helps. 希望这可以帮助。

cheers! 干杯! werd 编织

You can use a loop. 您可以使用循环。 If your language is PHP: 如果您的语言是PHP:

<?php
$patters=array('pattern1', 'pattern2', 'pattern3', 'pattern4', 'pattern5');
for($i=0;$i<5;$i++){
    echo '<div id="'.$patters[$i].'"><span class="a">Hackalicious</span></div>';
}
?>

Regards! 问候!

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

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