开发者问题收集

<a class='gotoLine' href='#230:45'>230:45</a> 未捕获的类型错误:

2021-10-26
1403

“230:45 未捕获 TypeError:无法读取未定义的属性(读取“shouldStopExecution”)”

我收到以下错误,您能修复它吗?我正尝试在同一页面上添加 HTML、CSS 和 javascript。

我在 codepen 中找到了此代码,我正在尝试解决该问题,但它不再起作用了……

但 javascript 在这里不起作用……

<div class="wrapper">
  <div class="poll-box">
    <div class="poll-container">
      <div class="poll-question">Do You Love to Play FreeFire?</div>
      <div class="poll-panel row mt-30">
        <div class="btn poll-panel-btn" aria-role="button" data-result="0" data-vote="0"> <span>Yes</span></div>
        <div class="btn poll-panel-btn" aria-role="button" data-result="0" data-vote="1"> <span>No</span></div>
      </div>
    </div>
  </div>
</div>






<style>* {
  box-sizing: border-box;
}

body {
  background: #1b1b1b;
  margin: 0;
}

.wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.mt-30 {
  margin: 30px 0 0 0;
}

.row, .column {
  display: flex;
}

.row {
  flex-direction: row;
}

.column {
  flex-direction: column;
}

.btn:not([disabled]) {
  cursor: pointer;
}

.poll-box {
  background: linear-gradient(#803af7, #500cc4);
  border-radius: 3px;
  box-shadow: 0px 5px 11px -7px #000;
  text-align: center;
}
.poll-container {
  padding: 25px 30px;
  position: relative;
}
.poll-question {
  width: max-content;
  max-width: 700px;
  color: #FFF;
  font-family: "Poppins", sans-serif;
}
.poll-panel.poll-voted {
  overflow: hidden;
  border-radius: 50px;
}
.poll-panel.poll-voted .poll-panel-btn.--user-choice {
  background: #FFF;
  color: #000;
}
.poll-panel.poll-voted .poll-panel-btn.--user-choice:hover {
  color: #000;
  background: #FFF;
}
.poll-panel.poll-voted .poll-panel-btn {
  background: #676464;
  color: #FFF;
  border-radius: 0;
  margin: 0;
  border: 0;
  position: relative;
}
.poll-panel.poll-voted .poll-panel-btn:hover {
  color: #FFF;
  background: #676464;
}
.poll-panel.poll-voted .poll-panel-btn:after {
  content: attr(data-result);
  font-size: 9px;
  display: block;
  opacity: 0.5;
  animation: slideWithFade 0.2s ease;
}
.poll-panel.poll-voted .poll-panel-btn:active {
  transform: inherit;
}
.poll-panel.poll-voted .poll-panel-btn span {
  display: block;
}
.poll-panel {
  width: 100%;
}
.poll-panel-btn {
  padding: 7px 10px;
  font-family: "Roboto", sans-serif;
  font-size: 0.7rem;
  width: 100%;
  border-radius: 50px;
  border: 1px solid #FFF;
  margin: 0 20px;
  color: #FFF;
  transition: 0.15s cubic-bezier(0.17, 0.67, 0.79, 1.24);
}
.poll-panel-btn:hover {
  background: #FFF;
  color: #000;
}
.poll-panel-btn:active {
  transform: scale(0.95);
}

@keyframes slideWithFade {
  0% {
    opacity: 0;
    transform: translateY(10px);
  }
  100% {
    opacity: 1;
  }
}</style>



<script>// POLL PLUGIN
class poll {
  constructor(question, answers, options) {
    const defaultOptions = {};
    this.options = Object.assign({}, defaultOptions, options);
    this.history = [];
    this.possibleAnswers = answers;
  }

  clear() {
    this.history = [];
  }

  get results() {
    let numberOfVotes = this.history.length,
    votesResults = [];

    Object.keys(this.possibleAnswers).forEach(answerId => {
      let answerIdCounter = 0;
      let voters = [];
      this.history.forEach(vote => {
        if (answerId == vote.id) {
          answerIdCounter++;
          voters.push(vote.name);
        }
      });
      let percentOfAllVotes = answerIdCounter / numberOfVotes * 100;
      let formatedPercent = isNaN(percentOfAllVotes) ?
      0 :
      parseFloat(percentOfAllVotes).
      toFixed(3).
      slice(0, -1);
      votesResults.push({
        votes: answerIdCounter,
        voters: voters,
        percent: formatedPercent });

    });

    return votesResults;
  }

  vote(answerId, name = "Anonymouse") {
    if (this.possibleAnswers[answerId]) {
      let getCurrentDate = new Date().toLocaleString();
      this.history.push({ id: answerId, name: name, date: getCurrentDate });
      return true;
    } else throw new Error("Incorrect answer's id");
  }}

// Plugin: https://codepen.io/badurski/pen/RJvJQZ

const q1 = new poll("Will Poland win the footboal match?", {
  0: { title: "Yes" },
  1: { title: "No" } });


// Add some randome votes
for (let i = 0; i < 20; i++) {if (window.CP.shouldStopExecution(0)) break;
  q1.vote(Math.floor(Math.random() * (1 - 0 + 1)) + 0);
}

// Poll interface script
window.CP.exitedLoop(0);let pollButtons = document.querySelectorAll('.poll-panel-btn'),
pollPanel = document.querySelector('.poll-panel');
pollButtons.forEach(button => {
  button.onclick = () => {
    if (button.getAttribute('disabled') != 'disabled') {
      q1.vote(button.dataset.vote);
      pollPanel.classList.add('poll-voted');
      button.classList.add('--user-choice');
      pollButtons.forEach(b => {
        b.setAttribute('disabled', 'disabled');
        let percent = q1.results[b.dataset.vote].percent + '%';
        b.style.width = percent;
        b.dataset.result = percent;
      });
    }
  };
});</script>
2个回答

在 codepen 中所有内容都是分开的,但在 HTML 中,您应该将脚本放在 div 之前。 类似:

<script>
   //blabla.yourscripts - you got it
</script>
<div class="wrapper">
   <div class="poll-box">
      <div class="poll-container">
         <div class="poll-question">Do You Love to Play FreeFire?</div>
         <div class="poll-panel row mt-30">
            <div class="btn poll-panel-btn" aria-role="button" data-result="0" data-vote="0"> <span>Yes</span></div>
            <div class="btn poll-panel-btn" aria-role="button" data-result="0" data-vote="1"> <span>No</span></div>
         </div>
      </div>
   </div>
</div>

因此,只需将脚本放在读取它们的内容之前即可。

Colly
2021-10-26

您包含了COP。

  • window.cp.exitedloop(0);
  • if(window.cp.shouldstopexecution(0))break;
  • 991855770

    Ankit Pundhir
    2021-10-27