为什么我的数组索引为零被跳过

loading...


0

我有一个subreddits数组,我想预先填入taginputs,但是,索引0处的元素一直被跳过 . 我不知道为什么 .

$.each(reddits, function(index, value) {
      $('#Tags').tagsinput('add', value);
      console.log(index);
    });

以上是负责重新填充taginputs(https://imgur.com/a/vjw2rMm)的代码,但它会跳过我的索引零元素 . 虽然在控制台上,它确实打印0:/

var reddits = [];
var exists = localStorage.getItem('redditList');
if(exists) {
    reddits = JSON.parse( exists );
}

$.each(reddits, function(index, value) {
  $('#Tags').tagsinput('add', value);
  console.log(index);
});

$('#Tags').tagsinput({
  confirmKeys: [13, 32]
});

$('input').on('itemAdded', function(event) {
  var sub = String(event.item);
  sub = sub.replace(/\s/g, '');
  reddits.push(sub);
  console.log(reddits);
  saveList();
});

$('input').on('itemRemoved', function(event) {
  reddits.splice(reddits.indexOf(event.item), 1);
  console.log(reddits);
  saveList();
});

function saveList() {
  localStorage.setItem('redditList', JSON.stringify(reddits));   // store it as string
  reddits = JSON.parse( localStorage.getItem('redditList'));     //  convert it to object
}

CSS

.inputContainer {
  width: 250px;
}
.bootstrap-tagsinput {
  background-color: #fff;
  border: 1px solid #ccc;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  display: inline-block;
  padding: 4px 6px;
  color: #555;
  vertical-align: middle;
  border-radius: 4px;
  width: 95%;
  line-height: 22px;
  cursor: text;
}
.bootstrap-tagsinput input {
  border: none;
  box-shadow: none;
  outline: none;
  background-color: transparent;
  padding: 0 6px;
  margin: 0;
  width: auto;
  max-width: inherit;
}
.bootstrap-tagsinput.form-control input::-moz-placeholder {
  color: #777;
  opacity: 1;
}
.bootstrap-tagsinput.form-control input:-ms-input-placeholder {
  color: #777;
}
.bootstrap-tagsinput.form-control input::-webkit-input-placeholder {
  color: #777;
}
.bootstrap-tagsinput input:focus {
  border: none;
  box-shadow: none;
}
.bootstrap-tagsinput .tag {
  background-color: #0abab5;
  display: inline;
  margin-right: 2px;
  padding: 0 5px;
  border-radius: 5px;
  color: black;
}
.bootstrap-tagsinput .tag [data-role="remove"] {
  margin-left: 8px;
  cursor: pointer;
}
.bootstrap-tagsinput .tag [data-role="remove"]:after {
  content: "x";
  padding: 0px 2px;
}
.bootstrap-tagsinput .tag [data-role="remove"]:hover {
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
}
.bootstrap-tagsinput .tag [data-role="remove"]:hover:active {
  box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
}

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Simple Chrome Extension</title>
  <link rel="stylesheet" type="text/css" media="screen" href="css/bootstrap-tagsinput.css">
</head>
<body>
  <div class="inputContainer">
    <input type="text" id="Tags" class="form-control"/>
  </div>
  <script src="jquery-3.3.1.min.js"></script>
    <script src="bootstrap-tagsinput.js"></script>
  <script type="text/javascript" src="js/popup.js"></script>
</body>
</html>

loading...

0回答

No Data

评论

暂时没有评论!