咨询:如何让jQuery UI Accordion不将<script>视为可见输出内容
Hey Rudolf,
I get your problem—when you initialize the jQuery UI Accordion on that div, it’s treating the embedded script tag as part of the panel’s visible content, which is messing with your built-in JS functionality. Let’s fix that.
Here are two solid approaches:
1. Restructure the content before initializing Accordion (cleanest solution)
This method separates the script from the panel content so Accordion ignores it entirely. We’ll wrap the actual visible content in a container, then move the script outside the panel. Add this code before you initialize the Accordion:
// Process each accordion section to isolate scripts $('#accordion').find('h3').each(function() { const $header = $(this); // Grab all elements after this header until the next header const $followingElements = $header.nextUntil('h3'); // Split into visible content and scripts const $panelContent = $followingElements.not('script'); const $scripts = $followingElements.filter('script'); // Wrap visible content into a dedicated panel div $panelContent.wrapAll('<div class="accordion-panel"></div>'); // Move scripts right after the panel (they’ll still run normally) $scripts.insertAfter($panelContent.parent()); }); // Now initialize Accordion with our custom panel selector $('#accordion').accordion({ header: 'h3', panel: '.accordion-panel' });
This way, the script stays in the DOM and executes as intended, but it’s not part of the Accordion’s panel content—so no extra attributes get added to it, and it won’t show up as visible data.
2. Quick CSS hack (if restructuring isn’t feasible)
If you need a faster fix without modifying the JS initialization, add this CSS to hide any script tags inside the accordion:
#accordion script { display: none !important; }
This will prevent the script tag from being visible, though it’ll still be part of the panel’s DOM structure. It’s less clean than the first method, but works if you’re in a pinch.
Either solution should let your built-in JS functionality stay intact while keeping the Accordion from treating the script as visible content.
内容的提问来源于stack exchange,提问作者Rudolf Ladyzhenskii

