点击输入框时含其div隐藏+动态PHP表格点击输入框消失求助
Hey there! Let's work through these two frustrating problems you're having with your MySQL-driven PHP table and its edit input fields. Here's a step-by-step breakdown to get things working right:
1. First, Find What's Causing the Disappearance
Before fixing anything, you need to track down what code is actually hiding your table, input, or div. This is almost always a click event listener (vanilla JS or jQuery) somewhere in your codebase—like:
- A listener on the document that hides the table when clicking outside it
- A direct click handler on the table/parent div that triggers hiding
- Accidental CSS rules (like
:focusor:activestates) that setdisplay: noneorvisibility: hidden
Do a quick search in your JS/CSS files for phrases like style.display = 'none', hide(), or pointer-events to narrow it down.
2. Fix Input Field Pointer Events
Your initial pointer-events: none; on the table stopped the whole table from triggering clicks, but it also made all child elements (including inputs) inherit that behavior. To override this for your edit inputs:
/* Replace .dynamic-table with your actual table class/id */ .dynamic-table { pointer-events: none; } /* Re-enable pointer events specifically for edit inputs */ .dynamic-table input.edit-input { pointer-events: auto; /* Add any custom input styles here */ }
Make sure your input selector has higher specificity than the table selector (e.g., if your table uses an ID like #user-table, use #user-table input.edit-input to ensure the rule applies).
3. Stop Event Bubbling (The Critical Fix)
Even if your input can receive clicks now, the click event might still bubble up to the table/parent div and trigger the hidden logic. To block this, add a listener to your inputs that stops the event from propagating:
Vanilla JavaScript:
// Grab all edit inputs in the table const editInputs = document.querySelectorAll('.dynamic-table input.edit-input'); editInputs.forEach(input => { input.addEventListener('click', function(e) { e.stopPropagation(); // Keep the event from reaching parent elements }); });
jQuery:
$('.dynamic-table input.edit-input').on('click', function(e) { e.stopPropagation(); });
This ensures clicking the input won't trigger any hide actions attached to the table or its parent.
4. Fix the Input + Parent Div Disappearing Issue
For the problem where clicking the input hides itself and its parent div:
- Check if there's a direct click handler attached to the input/parent div that's triggering the hide action—remove or modify it if exists.
- Double-check your CSS for any rules targeting
input:focusordiv:focusthat setdisplay: none—this is a common accidental mistake. - If you're using form validation or auto-hide logic, make sure it's not incorrectly targeting these input fields.
Quick Troubleshooting Tip
If you're stuck, use your browser's DevTools:
- Right-click the input and select "Inspect"
- Go to the "Events" tab to see all attached event listeners
- Check the "Styles" tab to verify
pointer-events: autois applied to the input (and not overridden)
内容的提问来源于stack exchange,提问作者Chodri

