<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" defer="defer">
var $ = function() {
return new Base();
}
function Base() {
this.elements = [];
//获取ID节点
this.getId = function(id) {
this.elements.push(document.getElementById(id));
return this;
}
//获取元素节点
this.getTagName = function(tag) {
var tags = document.getElementsByTagName(tag);
for(var i = 0; i<tags.length; i++) {
this.elements.push(tags[i]);
}
return this;
}
}
Base.prototype.css = function(attr, value) {
for(var i =0; i<this.elements.length; i++) {
this.elements[i].style[attr] = value;
}
return this;
}
Base.prototype.html = function(str) {
for(var i = 0; i<this.elements.length; i++) {
this.elements[i].innerHTML = str;
}
return this;
}
Base.prototype.click = function(fn) {
for(var i = 0; i<this.elements.length; i++) {
this.elements[i].onclick = fn;
}
return this;
}
window.onload = function() {
$().getId('ljh').css('color', 'red').css('backgroundColor', 'black');
$().getTagName('p').css('color', 'green').html('标题').click(function() {
alert('a');
}).css('backgroundColor', 'pink');
}
</script>
</head>
<body>
<div id="ljh">javascript is fun!</div>
<p>段落</p>
<p>段落</p><p>段落</p>
</body>
</html>
学习笔记-博客前段-封装库-连缀
最新推荐文章于 2021-04-21 20:40:22 发布