Use css selectors to add the html tag 's action

用css selectors 绑定html tag的行为
等同于下面的语句:
<li>
	<a onclick="this.parentNode.removeChild(this)" href="#">
		Click me to delete me
	</a>
</li>

这里用到了Ben Nolan 在 http://bennolan.com/behaviour/ 上发布的Behaviour.js .

1 . html文件如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Example</title>
		<script type="text/javascript" src="behavior.js"></script>
		<script type="text/javascript" src="example.js"></script>
	</head>
	<body>
	<ul id="example">
		<li>
			<a href="/delete">Click me to delete me</a>
		</li>
	</ul>
	</body>
</html>

2. example.js文件如下:
var myrules = {
	'#example li' : function(el){
		el.onclick = function(){
			this.parentNode.removeChild(this);

		}
	}
};

Behaviour.register(myrules);


结论: 这样的结果会让view层更清洁.

[附]behavior.js: http://bennolan.com/behaviour/behaviour.js

你可能感兴趣的:(html,css)