gpt4 book ai didi

javascript - 此返回窗口无法运行 javascript

转载 作者:太空宇宙 更新时间:2023-11-04 11:01:20 25 4
gpt4 key购买 nike

我正在构建一个待办事项列表,我有一个函数可以删除一个调用并为选项卡添加一个以显示事件状态。我还有 3 个 div,其中一个需要在单击选项卡时激活。我有标签工作,但我无法同时获得 div。函数 getList 正在删除正确的类,但将另一个类添加到导航而不是 div。我确定它与“this”有关 我试过打电话和绑定(bind)但没有看。我还试图避免使用 Jquery 并使用 vanilla JS。对于为什么它指向窗口而不是我的列表有什么帮助吗?

编辑:我还创建了一个 JS Fiddle http://jsfiddle.net/sxuvvypp/embedded/result/

(function() {
var elems = document.querySelectorAll("nav li");
var listElems = document.querySelectorAll("div > ul");

var makeActive = function() {
for (var i = 0; i < elems.length; i++)
elems[i].classList.remove('active');

this.classList.add('active');
};

var getList = function() {
for (var i = 0; i < listElems.length; i++)
listElems[i].classList.remove('listActive');
console.log(this);
this.classList.add('listActive');
}

for (var i = 0; i < elems.length; i++)
elems[i].addEventListener('click', function() {
makeActive.call(this);
getList(); //Issue here
});
})();
body {
margin: 0;
padding: 0;
background-color: darkgrey;
font-family: Ariel, Charcoal, sans-serif;
}
#container {
width: 500px;
height: 525px;
background-color: #f8f7f5;
margin: 50px auto;
}
header {
background: rgba(18, 136, 255, 1);
background: -moz-linear-gradient(top, rgba(18, 136, 255, 1) 0%, rgba(8, 182, 245, 1) 100%);
background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(18, 136, 255, 1)), color-stop(100%, rgba(8, 182, 245, 1)));
background: -webkit-linear-gradient(top, rgba(18, 136, 255, 1) 0%, rgba(8, 182, 245, 1) 100%);
background: -o-linear-gradient(top, rgba(18, 136, 255, 1) 0%, rgba(8, 182, 245, 1) 100%);
background: -ms-linear-gradient(top, rgba(18, 136, 255, 1) 0%, rgba(8, 182, 245, 1) 100%);
background: linear-gradient(to bottom, rgba(18, 136, 255, 1) 0%, rgba(8, 182, 245, 1) 100%);
filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#b7deed', endColorstr='#08b6f5', GradientType=0);
margin: 0;
height: 60px;
line-height: 60px;
color: white;
text-align: center;
}
nav {
background-color: white;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
background-color: white;
overflow: hidden;
}
nav li {
float: left;
width: 33.1%;
border-right: 1px solid;
}
nav li:last-child {
border: none;
}
nav li a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
color: black;
}
.active {
background-color: #f8f7f5;
}
h1 {
margin: 0;
}
.list {
height: 250px;
overflow: hidden;
list-style: none;
display: none;
}
.input {
padding-left: 30px;
}
.listActive {
display: block;
}
/*.yesterday{

}

.tomorrow{
display: none;
}*/

input[type=checkbox] {
display: none;
}
input[type=checkbox] + label:before {
content: "";
display: inline-block;
width: 15px;
height: 15px;
vertical-align: middle;
margin-right: 8px;
background-color: #aaa;
box-shadow: inset 0px 2px 2px rgba(0, 0, 0, .3);
border-radius: 4px;
}
input[type=checkbox]:checked + label:before {
content: "\2714";
/* Tick */
color: white;
background-color: #666;
text-align: center;
line-height: 15px;
text-shadow: 0px 0px 3px #eee;
}
.menu {
border-top: 2px solid grey;
width: 500px;
}
.menu label,
.menu input {
display: block;
width: 150px;
float: left;
margin: 10px 0px;
}
.menu label {
text-align: right;
padding-right: 20px;
font-size: 20px;
}
br {
clear: left;
}
<!DOCTYPE html>
<html>

<head>
<title>To Do List</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
<div id="container">
<header>
<h1>TO DO LIST</h1>
</header>
<nav>
<ul>
<li class="navButton"><a href="#">Yesterday</a></li>
<li class="navButton active"><a href="#">Today</a></li>
<li class="navButton"><a href="#">Tomorrow</a></li>
</ul>
</nav>
<div class="yesterday">
<ul class="list" id="yesterday">
<li><input type="checkbox" id="cbox"><label for="cbox"></label>first</li>
</ul>
</div>
<div class="today">
<ul class="list listActive " id="list">
<li><input type="checkbox" id="cbox"><label for="cbox"></label>second</li>
</ul>
</div>
<div class="tomorrow">
<ul class="list" id="tomorrow">
<li><input type="checkbox" id="cbox"><label for="cbox"></label>third</li>
</ul>
</div>
<div class="menu">
<label>Add Task:</label>
<input type="text" id="item">
<br>
<label>Date complete:</label>
<input type="text" id="date">
<br>
<input type="button" value="Add" id="addButton">
<input type="button" value="remove" id="removeButton">
</div>

</div>

<script type="text/javascript" src="todo.js"></script>
</body>

</html>

最佳答案

由于 JavaScript 范围的工作方式,全局存在的函数自然会指向窗口对象。这是因为,在幕后,全局函数声明如下所示:window.getList

针对您的情况的解决方法是将 this 作为 getList 函数的参数传递:

var getList = function (that){
for (var i = 0; i < listElems.length; i++)
listElems[i].classList.remove('listActive');
that.classList.add('listActive'); //apply the passed in this value as `that`
}



for (var i = 0; i < elems.length; i++)
elems[i].addEventListener('click', function(){
makeActive.call(this);
getList(this); //Pass in `this` value
});

关于javascript - 此返回窗口无法运行 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34277902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com