假设 $ 不是浏览器。现在要实现$,它会接受一个字符串,这是一个查询,它会使用querySelector来选择元素。(参考:https ://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector )
$('text')
现在实现 jquery 之类的函数 addClass 和 removeClass。(参考:https ://developer.mozilla.org/en-US/docs/Web/API/Element/classList )
$('#test').removeClass('blue').addClass('red');
现在实现 jquery 之类的函数延迟。(参考:https ://api.jquery.com/delay/ )在这里我被卡住了,无法为这种情况实施延迟。
$('#test').removeClass('blue').delay(2000).delay(1000).addClass('red');
代码示例
function $(selector) {
let element = document.querySelector(selector)
Object.prototype.addClass = function (className) {
this.classList.add(className)
return this
}
Object.prototype.removeClass = function (className) {
this.classList.remove(className)
return this
}
Object.prototype.delay = function(ms){
// what to do?
return this
}
return element
}
$('#test').removeClass('blue').delay(2000).delay(1000).addClass('red');
<!DOCTYPE html>
<html>
<head>
<style>
.blue{
background-color: blue;
}
.red{
background-color: red;
}
</style>
</head>
<body>
<div id="test" style="width: 200px; height: 200px;" class="blue"></div>
</body>
</html>
梦里花落0921
皈依舞
白板的微信
相关分类