簡體   English   中英

全選<td>使用 javascript 和 jQuery 的元素

[英]Selecting all <td> elements using javascript and jQuery

我試圖選擇所有通過 Javascript 創建的<td>元素並向它們添加一個點擊事件。 在我的代碼中,我在創建 td 元素后將 click 事件偵聽器添加到 fillTable() 函數底部的所有 td,但是我收到一條錯誤消息,指出“addEventListener 不是函數”。 為什么這不起作用?

我也試過

getElementsByTagName("td").addEventListener("click", clickEvent);

但這也不起作用。 我確保在 dom 加載后包含它,所以我認為這是我選擇元素的方式。 任何幫助表示贊賞。 謝謝

 let categories = []; let catsAndClues = []; // create Jeopardy Title and Start/Reset button $("body").append(` <h1 id="title">JEOPARDY!</h1> <div id="button-div"> <button id="button" data-startBtn="true">Start!</button> </div> <div id="game-board"> </div>`) async function getCategoryIds() { // save random number from one to 18000 to randomInt // randomInt will be used as the "offset" parameter to get a random sequence of categories let randomInt = Math.floor((Math.random() * 18000) + 1); let res = await axios.get(`http://jservice.io/api/categories?count=100&offset=${randomInt}`); // create a loop to iterate until the categories array contains 6 items for (let i=0;categories.length<6;i++){ // pull random ID number from the 100 categories pulled from API let i= Math.floor((Math.random() * 100) + 1); let randomCatId= res.data[i].id; // if categories array does not include the randomCatId, add it to the categories array if (!categories.includes(randomCatId)){ categories.push(randomCatId); } console.log(categories); } } async function getCategory(catId) { // retreive clues from API with the category ID parameter let res = await axios.get(`http://jservice.io/api/clues?category=${catId}`); // use .map function to return object displaying question, answer, and "showing" // properties for every item in the data's array let clueGroup = res.data.map(clue => { return { question: clue.question, answer: clue.answer, showing: null, }; }) console.log("clueGroup:", clueGroup); let clueArray = []; for (let i=0;clueArray.length<5;i++){ // pull random clue from the clues array and save to variable let i= Math.floor((Math.random() * clueGroup.length)); let randomClue= clueGroup[i]; // if categories array does not include the randomCatId, add it to the categories array if (!clueArray.includes(randomClue)){ clueArray.push(randomClue); } }; // define obj to show category title and list of all clues within the category console.log("clueArray: ", clueArray); console.log(res.data[0].category.title); return {title: res.data[0].category.title, clues: clueArray}; } function fillTable() { $("#game-board").append( `<table id="table"> <thead> <tr id="header-row"> <th>${catsAndClues[0].title}</th> <th>${catsAndClues[1].title}</th> <th>${catsAndClues[2].title}</th> <th>${catsAndClues[3].title}</th> <th>${catsAndClues[4].title}</th> <th>${catsAndClues[5].title}</th> </tr> </thead> <tbody> <tr> <td data-question= "${catsAndClues[0].clues[0].question}" data-answer= "${catsAndClues[0].clues[0].answer}" data-showing= "${catsAndClues[0].clues[0].showing}">? </td> <td data-question= "${catsAndClues[1].clues[0].question}" data-answer= "${catsAndClues[1].clues[0].answer}" data-showing= "${catsAndClues[1].clues[0].showing}">? </td> <td data-question= "${catsAndClues[2].clues[0].question}" data-answer= "${catsAndClues[2].clues[0].answer}" data-showing= "${catsAndClues[2].clues[0].showing}">? </td> <td data-question= "${catsAndClues[3].clues[0].question}" data-answer= "${catsAndClues[3].clues[0].answer}" data-showing= "${catsAndClues[3].clues[0].showing}">? </td> <td data-question= "${catsAndClues[4].clues[0].question}" data-answer= "${catsAndClues[4].clues[0].answer}" data-showing= "${catsAndClues[4].clues[0].showing}">? </td> <td data-question= "${catsAndClues[5].clues[0].question}" data-answer= "${catsAndClues[5].clues[0].answer}" data-showing= "${catsAndClues[5].clues[0].showing}">? </td> </tr> <tr> <td data-question= "${catsAndClues[0].clues[1].question}" data-answer= "${catsAndClues[0].clues[1].answer}" data-showing= "${catsAndClues[0].clues[1].showing}">? </td> <td data-question= "${catsAndClues[1].clues[1].question}" data-answer= "${catsAndClues[1].clues[1].answer}" data-showing= "${catsAndClues[1].clues[1].showing}">? </td> <td data-question= "${catsAndClues[2].clues[1].question}" data-answer= "${catsAndClues[2].clues[1].answer}" data-showing= "${catsAndClues[2].clues[1].showing}">? </td> <td data-question= "${catsAndClues[3].clues[1].question}" data-answer= "${catsAndClues[3].clues[1].answer}" data-showing= "${catsAndClues[3].clues[1].showing}">? </td> <td data-question= "${catsAndClues[4].clues[1].question}" data-answer= "${catsAndClues[4].clues[1].answer}" data-showing= "${catsAndClues[4].clues[1].showing}">? </td> <td data-question= "${catsAndClues[5].clues[1].question}" data-answer= "${catsAndClues[5].clues[1].answer}" data-showing= "${catsAndClues[5].clues[1].showing}">? </td> </tr> <tr> <td data-question= "${catsAndClues[0].clues[2].question}" data-answer= "${catsAndClues[0].clues[2].answer}" data-showing= "${catsAndClues[0].clues[2].showing}">? </td> <td data-question= "${catsAndClues[1].clues[2].question}" data-answer= "${catsAndClues[1].clues[2].answer}" data-showing= "${catsAndClues[1].clues[2].showing}">? </td> <td data-question= "${catsAndClues[2].clues[2].question}" data-answer= "${catsAndClues[2].clues[2].answer}" data-showing= "${catsAndClues[2].clues[2].showing}">? </td> <td data-question= "${catsAndClues[3].clues[2].question}" data-answer= "${catsAndClues[3].clues[2].answer}" data-showing= "${catsAndClues[3].clues[2].showing}">? </td> <td data-question= "${catsAndClues[4].clues[2].question}" data-answer= "${catsAndClues[4].clues[2].answer}" data-showing= "${catsAndClues[4].clues[2].showing}">? </td> <td data-question= "${catsAndClues[5].clues[2].question}" data-answer= "${catsAndClues[5].clues[2].answer}" data-showing= "${catsAndClues[5].clues[2].showing}">? </td> </tr> <tr> <td data-question= "${catsAndClues[0].clues[3].question}" data-answer= "${catsAndClues[0].clues[3].answer}" data-showing= "${catsAndClues[0].clues[3].showing}">? </td> <td data-question= "${catsAndClues[1].clues[3].question}" data-answer= "${catsAndClues[1].clues[3].answer}" data-showing= "${catsAndClues[1].clues[3].showing}">? </td> <td data-question= "${catsAndClues[2].clues[3].question}" data-answer= "${catsAndClues[2].clues[3].answer}" data-showing= "${catsAndClues[2].clues[3].showing}">? </td> <td data-question= "${catsAndClues[3].clues[3].question}" data-answer= "${catsAndClues[3].clues[3].answer}" data-showing= "${catsAndClues[3].clues[3].showing}">? </td> <td data-question= "${catsAndClues[4].clues[3].question}" data-answer= "${catsAndClues[4].clues[3].answer}" data-showing= "${catsAndClues[4].clues[3].showing}">? </td> <td data-question= "${catsAndClues[5].clues[3].question}" data-answer= "${catsAndClues[5].clues[3].answer}" data-showing= "${catsAndClues[5].clues[3].showing}">? </td> </tr> <tr> <td data-question= "${catsAndClues[0].clues[4].question}" data-answer= "${catsAndClues[0].clues[4].answer}" data-showing= "${catsAndClues[0].clues[4].showing}">? </td> <td data-question= "${catsAndClues[1].clues[4].question}" data-answer= "${catsAndClues[1].clues[4].answer}" data-showing= "${catsAndClues[1].clues[4].showing}">? </td> <td data-question= "${catsAndClues[2].clues[4].question}" data-answer= "${catsAndClues[2].clues[4].answer}" data-showing= "${catsAndClues[2].clues[4].showing}">? </td> <td data-question= "${catsAndClues[3].clues[4].question}" data-answer= "${catsAndClues[3].clues[4].answer}" data-showing= "${catsAndClues[3].clues[4].showing}">? </td> <td data-question= "${catsAndClues[4].clues[4].question}" data-answer= "${catsAndClues[4].clues[4].answer}" data-showing= "${catsAndClues[4].clues[4].showing}">? </td> <td data-question= "${catsAndClues[5].clues[4].question}" data-answer= "${catsAndClues[5].clues[4].answer}" data-showing= "${catsAndClues[5].clues[4].showing}">? </td> </tr> </tbody> </table>`); $("td").addEventListener("click", clickEvent); } // document.addEventListener("click", function(e){console.dir(e.target);}); // document.addEventListener("click", function(e){console.dir(e.target.dataset.question);}); // document.addEventListener("click", function(e){console.dir(e.target.dataset.answer);}); // document.addEventListener("click", function(e){console.dir(e.target.dataset.showing);}); /** Handle clicking on a clue: show the question or answer. * * Uses .showing property on clue to determine what to show: * - if currently null, show question & set .showing to "question" * - if currently "question", show answer & set .showing to "answer" * - if currently "answer", ignore click * */ function clickEvent(e) { let tile = e.target; let question = tile.dataset.question; let answer = tile.dataset.answer; let showing = tile.dataset.showing; console.log(question, answer, showing); // if (tile.showing == "answer"){ // return}; // if (!showing){ // tile.innerHTML = question; // showing = "question" // } // else if (showing == question){ // tile.innerHTML = answer; // showing = "answer" // } // else {return} } async function setupAndStart() { await getCategoryIds(); console.log(catsAndClues); for (let i=0;catsAndClues.length<6;i++){ let tempVar = await getCategory(categories[i]); catsAndClues[i] = tempVar; } console.log(catsAndClues); fillTable(); } setupAndStart()
 /* some colors you may find useful: #115ff4 #060ce9 #28a200 #8d2ab5 #74119c */ body { background-color: black; font-family: Helvetica; } #title { text-align: center; color: white; text-shadow: 4px 4px black; font-size: 5em; margin: 5px 5px 15px 5px; } #button-div{ text-align: center; margin-bottom: 2em; } #button{ padding: 5px 20px 5px 20px; font-family: Helvetica; } table { margin-left:auto; margin-right:auto; background-color: black; } th { text-transform: uppercase; border: 3px solid black; width: 150px; height: 100px; background-color: #060ce9; } td { text-align: center; width: 150px; height: 100px; vertical-align: middle; font-size: .5em; background-color: #060ce9; } table, td { border: 3px solid black; } #header-row { color: white; margin-bottom: 10px; font-size: 1em; }
 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title></title> <link href="https://fonts.googleapis.com/css?family=Copse&display=swap" rel="stylesheet"> <link rel="stylesheet" href="jeopardy.css"> </head> <body> <script src="https://unpkg.com/jquery"></script> <script src="https://unpkg.com/axios/dist/axios.js"></script> <script src="https://unpkg.com/lodash"></script> <script src="jeopardy.js"></script> </body> </html>

@Bolmstead,嘗試on代替addEventListener

$("td").addEventListener("click", clickEvent);

 let categories = []; let catsAndClues = []; // create Jeopardy Title and Start/Reset button $("body").append(` <h1 id="title">JEOPARDY!</h1> <div id="button-div"> <button id="button" data-startBtn="true">Start!</button> </div> <div id="game-board"> </div>`) async function getCategoryIds() { // save random number from one to 18000 to randomInt // randomInt will be used as the "offset" parameter to get a random sequence of categories let randomInt = Math.floor((Math.random() * 18000) + 1); let res = await axios.get(`http://jservice.io/api/categories?count=100&offset=${randomInt}`); // create a loop to iterate until the categories array contains 6 items for (let i=0;categories.length<6;i++){ // pull random ID number from the 100 categories pulled from API let i= Math.floor((Math.random() * 100) + 1); let randomCatId= res.data[i].id; // if categories array does not include the randomCatId, add it to the categories array if (!categories.includes(randomCatId)){ categories.push(randomCatId); } console.log(categories); } } async function getCategory(catId) { // retreive clues from API with the category ID parameter let res = await axios.get(`http://jservice.io/api/clues?category=${catId}`); // use .map function to return object displaying question, answer, and "showing" // properties for every item in the data's array let clueGroup = res.data.map(clue => { return { question: clue.question, answer: clue.answer, showing: null, }; }) console.log("clueGroup:", clueGroup); let clueArray = []; for (let i=0;clueArray.length<5;i++){ // pull random clue from the clues array and save to variable let i= Math.floor((Math.random() * clueGroup.length)); let randomClue= clueGroup[i]; // if categories array does not include the randomCatId, add it to the categories array if (!clueArray.includes(randomClue)){ clueArray.push(randomClue); } }; // define obj to show category title and list of all clues within the category console.log("clueArray: ", clueArray); console.log(res.data[0].category.title); return {title: res.data[0].category.title, clues: clueArray}; } function fillTable() { $("#game-board").append( `<table id="table"> <thead> <tr id="header-row"> <th>${catsAndClues[0].title}</th> <th>${catsAndClues[1].title}</th> <th>${catsAndClues[2].title}</th> <th>${catsAndClues[3].title}</th> <th>${catsAndClues[4].title}</th> <th>${catsAndClues[5].title}</th> </tr> </thead> <tbody> <tr> <td data-question= "${catsAndClues[0].clues[0].question}" data-answer= "${catsAndClues[0].clues[0].answer}" data-showing= "${catsAndClues[0].clues[0].showing}">? </td> <td data-question= "${catsAndClues[1].clues[0].question}" data-answer= "${catsAndClues[1].clues[0].answer}" data-showing= "${catsAndClues[1].clues[0].showing}">? </td> <td data-question= "${catsAndClues[2].clues[0].question}" data-answer= "${catsAndClues[2].clues[0].answer}" data-showing= "${catsAndClues[2].clues[0].showing}">? </td> <td data-question= "${catsAndClues[3].clues[0].question}" data-answer= "${catsAndClues[3].clues[0].answer}" data-showing= "${catsAndClues[3].clues[0].showing}">? </td> <td data-question= "${catsAndClues[4].clues[0].question}" data-answer= "${catsAndClues[4].clues[0].answer}" data-showing= "${catsAndClues[4].clues[0].showing}">? </td> <td data-question= "${catsAndClues[5].clues[0].question}" data-answer= "${catsAndClues[5].clues[0].answer}" data-showing= "${catsAndClues[5].clues[0].showing}">? </td> </tr> <tr> <td data-question= "${catsAndClues[0].clues[1].question}" data-answer= "${catsAndClues[0].clues[1].answer}" data-showing= "${catsAndClues[0].clues[1].showing}">? </td> <td data-question= "${catsAndClues[1].clues[1].question}" data-answer= "${catsAndClues[1].clues[1].answer}" data-showing= "${catsAndClues[1].clues[1].showing}">? </td> <td data-question= "${catsAndClues[2].clues[1].question}" data-answer= "${catsAndClues[2].clues[1].answer}" data-showing= "${catsAndClues[2].clues[1].showing}">? </td> <td data-question= "${catsAndClues[3].clues[1].question}" data-answer= "${catsAndClues[3].clues[1].answer}" data-showing= "${catsAndClues[3].clues[1].showing}">? </td> <td data-question= "${catsAndClues[4].clues[1].question}" data-answer= "${catsAndClues[4].clues[1].answer}" data-showing= "${catsAndClues[4].clues[1].showing}">? </td> <td data-question= "${catsAndClues[5].clues[1].question}" data-answer= "${catsAndClues[5].clues[1].answer}" data-showing= "${catsAndClues[5].clues[1].showing}">? </td> </tr> <tr> <td data-question= "${catsAndClues[0].clues[2].question}" data-answer= "${catsAndClues[0].clues[2].answer}" data-showing= "${catsAndClues[0].clues[2].showing}">? </td> <td data-question= "${catsAndClues[1].clues[2].question}" data-answer= "${catsAndClues[1].clues[2].answer}" data-showing= "${catsAndClues[1].clues[2].showing}">? </td> <td data-question= "${catsAndClues[2].clues[2].question}" data-answer= "${catsAndClues[2].clues[2].answer}" data-showing= "${catsAndClues[2].clues[2].showing}">? </td> <td data-question= "${catsAndClues[3].clues[2].question}" data-answer= "${catsAndClues[3].clues[2].answer}" data-showing= "${catsAndClues[3].clues[2].showing}">? </td> <td data-question= "${catsAndClues[4].clues[2].question}" data-answer= "${catsAndClues[4].clues[2].answer}" data-showing= "${catsAndClues[4].clues[2].showing}">? </td> <td data-question= "${catsAndClues[5].clues[2].question}" data-answer= "${catsAndClues[5].clues[2].answer}" data-showing= "${catsAndClues[5].clues[2].showing}">? </td> </tr> <tr> <td data-question= "${catsAndClues[0].clues[3].question}" data-answer= "${catsAndClues[0].clues[3].answer}" data-showing= "${catsAndClues[0].clues[3].showing}">? </td> <td data-question= "${catsAndClues[1].clues[3].question}" data-answer= "${catsAndClues[1].clues[3].answer}" data-showing= "${catsAndClues[1].clues[3].showing}">? </td> <td data-question= "${catsAndClues[2].clues[3].question}" data-answer= "${catsAndClues[2].clues[3].answer}" data-showing= "${catsAndClues[2].clues[3].showing}">? </td> <td data-question= "${catsAndClues[3].clues[3].question}" data-answer= "${catsAndClues[3].clues[3].answer}" data-showing= "${catsAndClues[3].clues[3].showing}">? </td> <td data-question= "${catsAndClues[4].clues[3].question}" data-answer= "${catsAndClues[4].clues[3].answer}" data-showing= "${catsAndClues[4].clues[3].showing}">? </td> <td data-question= "${catsAndClues[5].clues[3].question}" data-answer= "${catsAndClues[5].clues[3].answer}" data-showing= "${catsAndClues[5].clues[3].showing}">? </td> </tr> <tr> <td data-question= "${catsAndClues[0].clues[4].question}" data-answer= "${catsAndClues[0].clues[4].answer}" data-showing= "${catsAndClues[0].clues[4].showing}">? </td> <td data-question= "${catsAndClues[1].clues[4].question}" data-answer= "${catsAndClues[1].clues[4].answer}" data-showing= "${catsAndClues[1].clues[4].showing}">? </td> <td data-question= "${catsAndClues[2].clues[4].question}" data-answer= "${catsAndClues[2].clues[4].answer}" data-showing= "${catsAndClues[2].clues[4].showing}">? </td> <td data-question= "${catsAndClues[3].clues[4].question}" data-answer= "${catsAndClues[3].clues[4].answer}" data-showing= "${catsAndClues[3].clues[4].showing}">? </td> <td data-question= "${catsAndClues[4].clues[4].question}" data-answer= "${catsAndClues[4].clues[4].answer}" data-showing= "${catsAndClues[4].clues[4].showing}">? </td> <td data-question= "${catsAndClues[5].clues[4].question}" data-answer= "${catsAndClues[5].clues[4].answer}" data-showing= "${catsAndClues[5].clues[4].showing}">? </td> </tr> </tbody> </table>`); $("td").on("click", clickEvent); } // document.addEventListener("click", function(e){console.dir(e.target);}); // document.addEventListener("click", function(e){console.dir(e.target.dataset.question);}); // document.addEventListener("click", function(e){console.dir(e.target.dataset.answer);}); // document.addEventListener("click", function(e){console.dir(e.target.dataset.showing);}); /** Handle clicking on a clue: show the question or answer. * * Uses .showing property on clue to determine what to show: * - if currently null, show question & set .showing to "question" * - if currently "question", show answer & set .showing to "answer" * - if currently "answer", ignore click * */ function clickEvent(e) { let tile = e.target; let question = tile.dataset.question; let answer = tile.dataset.answer; let showing = tile.dataset.showing; console.log(question, answer, showing); // if (tile.showing == "answer"){ // return}; // if (!showing){ // tile.innerHTML = question; // showing = "question" // } // else if (showing == question){ // tile.innerHTML = answer; // showing = "answer" // } // else {return} } async function setupAndStart() { await getCategoryIds(); console.log(catsAndClues); for (let i=0;catsAndClues.length<6;i++){ let tempVar = await getCategory(categories[i]); catsAndClues[i] = tempVar; } console.log(catsAndClues); fillTable(); } setupAndStart()
 /* some colors you may find useful: #115ff4 #060ce9 #28a200 #8d2ab5 #74119c */ body { background-color: black; font-family: Helvetica; } #title { text-align: center; color: white; text-shadow: 4px 4px black; font-size: 5em; margin: 5px 5px 15px 5px; } #button-div{ text-align: center; margin-bottom: 2em; } #button{ padding: 5px 20px 5px 20px; font-family: Helvetica; } table { margin-left:auto; margin-right:auto; background-color: black; } th { text-transform: uppercase; border: 3px solid black; width: 150px; height: 100px; background-color: #060ce9; } td { text-align: center; width: 150px; height: 100px; vertical-align: middle; font-size: .5em; background-color: #060ce9; } table, td { border: 3px solid black; } #header-row { color: white; margin-bottom: 10px; font-size: 1em; }
 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title></title> <link href="https://fonts.googleapis.com/css?family=Copse&display=swap" rel="stylesheet"> <link rel="stylesheet" href="jeopardy.css"> </head> <body> <script src="https://unpkg.com/jquery"></script> <script src="https://unpkg.com/axios/dist/axios.js"></script> <script src="https://unpkg.com/lodash"></script> <script src="jeopardy.js"></script> </body> </html>

實現相同結果的另一種方法是在父表上使用.on()應用“委托事件偵聽”:

$('#table').on('click','td', function(ev){...})

您甚至可以填充表格之前執行此操作,因為它將點擊事件從表格委托給內部的每個當前和未來<TD>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM