亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Maths Capture 需要添加到同一 html 頁面上的兩個選項卡?

Maths Capture 需要添加到同一 html 頁面上的兩個選項卡?

鴻蒙傳說 2022-12-29 14:14:59
我附上了一個顯示代碼的片段。我正在嘗試在同一 HTML 頁面上的兩個不同選項卡上添加兩個 Math Capture。因此,在“新聞”選項卡上,捕獲工作正常,但在“聯系方式”選項卡上,數學問題不會只顯示一個空白框。但是聯系人選項卡中的提交按鈕在新聞選項卡中有效,但它使用新聞選項卡中的數學問題是錯誤的。我不確定如何讓它在兩個頁面上都起作用?希望你能幫忙??謝謝蒂姆function openPage(pageName, elmnt, color) {  // Hide all elements with class="tabcontent" by default */  var i, tabcontent, tablinks;  tabcontent = document.getElementsByClassName("tabcontent");  for (i = 0; i < tabcontent.length; i++) {    tabcontent[i].style.display = "none";  }  // Remove the background color of all tablinks/buttons  tablinks = document.getElementsByClassName("tablink");  for (i = 0; i < tablinks.length; i++) {    tablinks[i].style.backgroundColor = "";  }  // Show the specific tab content  document.getElementById(pageName).style.display = "block";  // Add the specific color to the button used to open the tab content  elmnt.style.backgroundColor = color;}// Get the element with id="defaultOpen" and click on itdocument.getElementById("defaultOpen").click();var total;function getRandom(){return Math.ceil(Math.random()* 20);}function createSum(){        var randomNum1 = getRandom(),            randomNum2 = getRandom();    total =randomNum1 + randomNum2;  $( "#question" ).text( randomNum1 + " + " + randomNum2 + "=" );    $("#ans").val('');  checkInput();}function checkInput(){        var input = $("#ans").val(),         slideSpeed = 200,      hasInput = !!input,       valid = hasInput && input == total;    $('#message').toggle(!hasInput);    $('button[type=submit]').prop('disabled', !valid);      $('#success').toggle(valid);    $('#fail').toggle(hasInput && !valid);}$(document).ready(function(){    //create initial sum    createSum();    // On "reset button" click, generate new random sum    $('button[type=reset]').click(createSum);    // On user input, check value    $( "#ans" ).keyup(checkInput);});
查看完整描述

1 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

主要問題是您對多個元素使用相同的 id,id應該是唯一的。我的建議是使用class并使選擇器“相對于”可見選項卡


const currentPage = $('.tabcontent:visible');

currentPage.find('.message').toggle(!hasInput); // for example

createSum何時調用每個函數 (和checkInput)有更多變化。


請查看下面的代碼,并指出是否有不明確或未按預期工作的地方。


function openPage(pageName, elmnt, color) {

  // Hide all elements with class="tabcontent" by default */

  var i, tabcontent, tablinks;

  tabcontent = document.getElementsByClassName("tabcontent");

  for (i = 0; i < tabcontent.length; i++) {

    tabcontent[i].style.display = "none";

  }


  // Remove the background color of all tablinks/buttons

  tablinks = document.getElementsByClassName("tablink");

  for (i = 0; i < tablinks.length; i++) {

    tablinks[i].style.backgroundColor = "";

  }


  // Show the specific tab content

  document.getElementById(pageName).style.display = "block";


  // Add the specific color to the button used to open the tab content

  elmnt.style.backgroundColor = color;

  createSum();

}


// Get the element with id="defaultOpen" and click on it

document.getElementById("defaultOpen").click();



var total;


function getRandom() {

  return Math.ceil(Math.random() * 20);

}


function createSum() {

  var randomNum1 = getRandom(),

    randomNum2 = getRandom();

  total = randomNum1 + randomNum2;

  

  const currentPage = $('.tabcontent:visible');

  currentPage.find(".question").text(randomNum1 + " + " + randomNum2 + "=");

  currentPage.find(".question").val('');

  currentPage.find('button[type=submit]').prop('disabled', true);

}


function checkInput() {

  var input = $(this).val(),

   slideSpeed = 200,

   hasInput = !!input,

   valid = hasInput && input == total;


  const currentPage = $('.tabcontent:visible');

  currentPage.find('.message').toggle(!hasInput);

  currentPage.find('button[type=submit]').prop('disabled', !valid);

  currentPage.find('.success').toggle(valid);

  currentPage.find('.fail').toggle(hasInput && !valid);

}


$(document).ready(function() {

  // On "reset button" click, generate new random sum

  $('button[type=reset]').click(createSum);

  // On user input, check value

  $(".ans").keyup(checkInput);

});

body,

html {

  height: 100%;

  margin: 0;

  font-family: Arial;

}



/* Style tab links */


.tablink {

  background-color: #555;

  color: white;

  float: left;

  border: none;

  outline: none;

  cursor: pointer;

  padding: 14px 16px;

  font-size: 17px;

  width: 25%;

}


.success,

.fail {

  display: none;

}


.message,

.success,

.fail {

  margin-top: 10px;

  margin-bottom: 10px;

}


.container {

  position: absolute;

  left: 50%;

  top: 50%;

  transform: translate(-50%, -50%);

}


p {

  display: inline;

  margin-right: 5px;

}


input,

button {

  font-family: 'Open Sans';

  font-weight: lighter;

  font-size: 12px;

}


input {

  border: 1px solid #FFBBD7;

  width: 30px;

  height: 20px;

  text-align: center;

}


button {

  border: none;

  border-radius: 1.5em;

  color: #FFF;

  background: #FFBBD7;

  padding: 2.5px 10px;

  width: 75px;

  height: 30px;

  cursor: pointer;

  transition: background .5s ease-in-out;

}


button:hover:enabled {

  background: #303030;

}


button:disabled {

  opacity: .5;

  cursor: default;

}


.tablink:hover {

  background-color: #777;

}



/* Style the tab content (and add height:100% for full page content) */


.tabcontent {

  color: white;

  display: none;

  padding: 100px 20px;

  height: 100%;

}


#Home {

  background-color: red;

}


#News {

  background-color: green;

}


#Contact {

  background-color: blue;

}


#About {

  background-color: orange;

}

<title>captcha</title>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

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

<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">

<link rel="stylesheet" type="text/css" href="style.css">


<button class="tablink" onclick="openPage('Home', this, 'red')">Home</button>

<button class="tablink" onclick="openPage('News', this, 'green')" id="defaultOpen">News</button>

<button class="tablink" onclick="openPage('Contact', this, 'blue')">Contact</button>

<button class="tablink" onclick="openPage('About', this, 'orange')">About</button>


<div id="Home" class="tabcontent">

  <h3>Home</h3>

  <p>Home is where the heart is..</p>

</div>


<div id="News" class="tabcontent">

  <h3>News</h3>

  <form>

    <p class="question"></p><input class="ans" type="text">

    <div class="message">Please verify.</div>

    <div class="success">Validation complete :)</div>

    <div class="fail">Validation failed :(</div>

    <button type="submit" value="submit">Submit</button>

    <button type="reset" value="reset">Reset</button>

  </form>

</div>


<div id="Contact" class="tabcontent">

  <h3>Contact</h3>

  <form>

    <p class="question"></p><input class="ans" type="text">

    <div class="message">Please verify.</div>

    <div class="success">Validation complete :)</div>

    <div class="fail">Validation failed :(</div>

    <button type="submit" value="submit">Submit</button>

    <button type="reset" value="reset">Reset</button>

  </form>

</div>


<div id="About" class="tabcontent">

  <h3>About</h3>

  <p>Who we are and what we do.</p>

</div>


查看完整回答
反對 回復 2022-12-29
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號