如何按列表遍历表格tr、拆分登录账号与密码并在表格中打印两者值
Hey there! Let's tackle your questions step by step, plus fix those code bugs you mentioned.
1. How to Iterate Through <tr> Elements in a Table
You can use native JavaScript DOM methods to grab all table rows and loop through them. Here's a straightforward example:
// Grab all <tr> elements in your table (add :not(:first-child) if you want to skip the header row) const tableRows = document.querySelectorAll('table tr'); // Loop through each row tableRows.forEach((row, index) => { console.log(`Row ${index + 1}:`, row); // Add your row-specific logic here, like grabbing <td> content });
If you're using jQuery, it's just $('table tr').each(function() { /* your code */ }); but native JS is more lightweight these days.
2. How to Split Login ID and Password
First, you need a consistent separator in your combined string (like |, :, or a space). Use the split() method to break it into two parts:
// Example: Combined string with a | separator const combinedCredentials = "jane_smith|myStrongPass789"; // Destructure the split result into loginId and password const [loginId, password] = combinedCredentials.split('|'); console.log('Login ID:', loginId); console.log('Password:', password);
Add a quick validation to handle bad formatting:
function splitCredentials(str, separator = '|') { const parts = str.split(separator); if (parts.length !== 2) { throw new Error("Invalid format! Make sure your login ID and password are separated by the correct character."); } return { loginId: parts[0], password: parts[1] }; }
3. How to Print Login ID & Password to a Table
You can create new table rows and cells dynamically, then append them to your table. Note: Never display plaintext passwords in production—this is just for demo purposes!
// Grab your target table const targetTable = document.querySelector('table'); // Create a new row const newRow = document.createElement('tr'); // Create Login ID cell const loginCell = document.createElement('td'); loginCell.textContent = loginId; newRow.appendChild(loginCell); // Create Password cell (again, hide this in real apps!) const passwordCell = document.createElement('td'); passwordCell.textContent = password; newRow.appendChild(passwordCell); // Add the new row to the table targetTable.appendChild(newRow);
If you need to update an existing row instead, find the row by index or ID, then modify its <td> elements' textContent.
Fixes for Your Existing Code Issues
Let's fix those bugs you pointed out:
Reusing loginIdVal for password element:
Wrong:const password = document.getElementById(loginIdVal); // Using login ID value instead of password element IDFixed:
// Replace 'password' with your actual password element's ID const passwordElement = document.getElementById('password'); const passwordVal = passwordElement.value;Using
pushinstead oflistin alert:
Wrong:alert('List content: ' + push); // Typo—should reference your list variableFixed:
// Use join() to format the list nicely alert('List content: ' + list.join(', '));Misspelled
methodein form tag:
Wrong:<form methode="POST">...</form>Fixed:
<form method="POST">...</form> <!-- Correct spelling is "method" -->
内容的提问来源于stack exchange,提问作者Samudrala Ramu

