前端开发如何实现结构、样式、行为彻底分离
2017-06-04 10:24
696 查看
严格意义上来讲,彻底分离就是结构只出现HTML的代码,没有任何的CSS、JS代码,甚至通过行为改变的HTML也应该写在JS代码里面。
HTML:index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>image gallery</title>
<link rel="stylesheet" href="layout/index.css" media="screen">
</head>
<body>
<h1>Snapshots</h1>
<ul id="imagegallery">
<li>
<a href="image/a.png" title="a">
<img src="image/a.png">
</a>
</li>
<li>
<a href="image/b.png" title="b">
<img src="image/b.png">
</a>
</li>
<li>
<a href="image/c.png" title="c">
<img src="image/c.png">
</a>
</li>
<li>
<a href="image/d.png" title="d">
<img src="image/d.png">
</a>
</li>
</ul>
<!--<img id="placeholder" src="image/p.png" alt="my image" />-->
<!--<p id="description"> choose an image</p>-->
<!--<div id="testid"></div>-->
<script src="scripts/index.js"></script>
</body>
</html>
CSS:index.css
body{
font-family: "Helvetica", "Arial", serif;
color: #333;
background-color: #ccc;
margin: 1em 10%;
}
h1{
color: #333;
background-color: transparent;
}
a{
color: #c60;
background-color: transparent;
font-weight: bold;
text-decoration: none;
}
ul{
padding: 0;
}
li{
float: left;
padding: 1em;
list-style: none;
}
#imagegallery{
list-style: none;
}
#imagegallery li{
list-style: inline;
}
#imagegallery li a img{
border:0;
}
#placeholder{
width: 50px;
height: 50px;
}
JavaScript:index.js
function addLoadEvent(func){
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func;
}else {
window.onload = function(){
oldonload();
func();
}
}
}
function insertAfter(newElement,targetElement){
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
parent.appendChild(newElement);
}else {
parent.insertBefore(newElement, targetElement.nextSibling);
}
}
function preparePlaceholder(){
if (!document.createElement) return false;
if (!document.createTextNode) return false;
if (!document.getElementById) return false;
if (!document.getElementById("imagegallery")) return false;
var placeholder = document.createElement("img");
placeholder.setAttribute("id","placeholder");
placeholder.setAttribute("src","image/p.png");
placeholder.setAttribute("alt","my image gallery");
var description = document.createElement("p");
description.setAttribute("id","description");
var destext = document.createTextNode("choose one image");
description.appendChild(destext);
//document.getElementsByTagName("body")[0].appendChild(placeholder);
//document.getElementsByTagName("body")[0].appendChild(description);
var gallery = document.getElementById("imagegallery");
//gallery.parentNode.insertBefore(placeholder,gallery);
//gallery.parentNode.insertBefore(description,gallery);
insertAfter(description,gallery);
insertAfter(placeholder,gallery);
}
function prepareGallery(){
if (!document.getElementsByTagName) return false;
if (!document.getElementById) return false;
if (!document.getElementById("imagegallery")) return false;
var gallery = document.getElementById("imagegallery");
var links = gallery.getElementsByTagName("a");
for(var i=0; i<links.length; i++){
links[i].onclick = function(){
return showPic(this) ? false : true;
}
}
}
function showPic(whichpic){
if (!document.getElementById("placeholder")) return false;
var source = whichpic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
placeholder.setAttribute("src", source);
if (document.getElementById("description")){
var text = whichpic.getAttribute("title") ? whichpic.getAttribute("title") : "";
var description = document.getElementById("description");
if (description.firstChild.nodeType == 3){
description.firstChild.nodeValue = text;
}
}
return true;
}
addLoadEvent(preparePlaceholder);
addLoadEvent(prepareGallery);
HTML:index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>image gallery</title>
<link rel="stylesheet" href="layout/index.css" media="screen">
</head>
<body>
<h1>Snapshots</h1>
<ul id="imagegallery">
<li>
<a href="image/a.png" title="a">
<img src="image/a.png">
</a>
</li>
<li>
<a href="image/b.png" title="b">
<img src="image/b.png">
</a>
</li>
<li>
<a href="image/c.png" title="c">
<img src="image/c.png">
</a>
</li>
<li>
<a href="image/d.png" title="d">
<img src="image/d.png">
</a>
</li>
</ul>
<!--<img id="placeholder" src="image/p.png" alt="my image" />-->
<!--<p id="description"> choose an image</p>-->
<!--<div id="testid"></div>-->
<script src="scripts/index.js"></script>
</body>
</html>
CSS:index.css
body{
font-family: "Helvetica", "Arial", serif;
color: #333;
background-color: #ccc;
margin: 1em 10%;
}
h1{
color: #333;
background-color: transparent;
}
a{
color: #c60;
background-color: transparent;
font-weight: bold;
text-decoration: none;
}
ul{
padding: 0;
}
li{
float: left;
padding: 1em;
list-style: none;
}
#imagegallery{
list-style: none;
}
#imagegallery li{
list-style: inline;
}
#imagegallery li a img{
border:0;
}
#placeholder{
width: 50px;
height: 50px;
}
JavaScript:index.js
function addLoadEvent(func){
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func;
}else {
window.onload = function(){
oldonload();
func();
}
}
}
function insertAfter(newElement,targetElement){
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
parent.appendChild(newElement);
}else {
parent.insertBefore(newElement, targetElement.nextSibling);
}
}
function preparePlaceholder(){
if (!document.createElement) return false;
if (!document.createTextNode) return false;
if (!document.getElementById) return false;
if (!document.getElementById("imagegallery")) return false;
var placeholder = document.createElement("img");
placeholder.setAttribute("id","placeholder");
placeholder.setAttribute("src","image/p.png");
placeholder.setAttribute("alt","my image gallery");
var description = document.createElement("p");
description.setAttribute("id","description");
var destext = document.createTextNode("choose one image");
description.appendChild(destext);
//document.getElementsByTagName("body")[0].appendChild(placeholder);
//document.getElementsByTagName("body")[0].appendChild(description);
var gallery = document.getElementById("imagegallery");
//gallery.parentNode.insertBefore(placeholder,gallery);
//gallery.parentNode.insertBefore(description,gallery);
insertAfter(description,gallery);
insertAfter(placeholder,gallery);
}
function prepareGallery(){
if (!document.getElementsByTagName) return false;
if (!document.getElementById) return false;
if (!document.getElementById("imagegallery")) return false;
var gallery = document.getElementById("imagegallery");
var links = gallery.getElementsByTagName("a");
for(var i=0; i<links.length; i++){
links[i].onclick = function(){
return showPic(this) ? false : true;
}
}
}
function showPic(whichpic){
if (!document.getElementById("placeholder")) return false;
var source = whichpic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
placeholder.setAttribute("src", source);
if (document.getElementById("description")){
var text = whichpic.getAttribute("title") ? whichpic.getAttribute("title") : "";
var description = document.getElementById("description");
if (description.firstChild.nodeType == 3){
description.firstChild.nodeValue = text;
}
}
return true;
}
addLoadEvent(preparePlaceholder);
addLoadEvent(prepareGallery);
相关文章推荐
- 实现lightBox时的样式与行为分离减少JS第1/2页
- Javascript 笔记与总结(2-15)结构、样式、行为分离
- 结构和样式,行为的分离
- 使用laravel开发网站时,如何实现前端导航栏共享数据二种简单的方法
- javascript实现表现、结构、行为分离的选项卡效果!
- 前后端分离开发背景下,前端如何使用nginx代理转发ajax跨域访问后台服务?
- 谈谈web前端结构与行为的分离
- flash开发中如何实现界面代码分离
- 关于web开发中如何彻底分离美工和后台程序工作的随想
- 如何实现前后端分离开发
- javascript实现表现、结构、行为分离的选项卡效果!
- J2EE 关于Web前端与服务端实现开发分离的实践 .
- 前端开发中如何实现异步编程
- J2EE 关于Web前端与服务端实现开发分离的实践
- 结构-行为-样式 - Angularjs 环境下Ztree结合JqueryUI实现拖拽
- 网页结构层、表示层和行为层的彻底分离
- 前端开发:css技巧,如何设置select、radio 、 checkbox 、file这些不可直接设置的样式 。
- .Net语言 APP开发平台——Smobiler学习日志:如何实现微信朋友圈的消息样式?
- bootstrap开发,实现前端后台模块分离
- 结构和样式,行为的分离