IE11中a标签target属性无法切换至已打开标签页问题求助
Fix for IE11 Not Switching to Already Opened Tab When Clicking Link with Custom Target
First, let's confirm your scenario: you're using this HTML code to try reusing the same tab for your link:
<!DOCTYPE html> <html> <body> <a href="https://www.w3schools.com" target="_yoy">click me</a> </body> </html>
This works perfectly in Chrome and Firefox—clicking the link reuses the tab named _yoy as expected. But in IE11, the first click opens a new tab, and subsequent clicks only highlight that tab without switching to it. This is a known compatibility bug in IE11's handling of custom target attribute values.
Solution: Control Tab Switching Manually with JavaScript
IE11 doesn't auto-switch to the highlighted tab, but it does support the window.focus() method. We can use JavaScript to maintain a reference to the opened window and force the switch:
<!DOCTYPE html> <html> <body> <a href="https://www.w3schools.com" onclick="return handleTabSwitch(this);">click me</a> <script> // Keep a reference to the target window let targetTab = null; function handleTabSwitch(linkElement) { // Check if the window exists and hasn't been closed if (targetTab && !targetTab.closed) { // Force switch to the existing tab targetTab.focus(); } else { // Open a new tab and save its reference targetTab = window.open(linkElement.href, '_yoy'); } // Prevent default link behavior to avoid duplicate tabs return false; } </script> </body> </html>
Why This Works
- We use a global variable
targetTabto keep track of the opened window. Every click checks if this window is still active. - If the window exists,
focus()forces IE11 to switch to it—an operation the browser responds to correctly. - If the user closed the tab, the script opens a new one and updates the reference.
- This solution also works seamlessly in Chrome and Firefox, so you don't lose existing functionality.
Extra Notes
- Make sure the
hrefof the link matches the URL used inwindow.open—if they differ, the existing tab will navigate to the new URL, which is standard reuse behavior. - For multiple custom targets, just create separate window references for each unique target name.
内容的提问来源于stack exchange,提问作者Rajib Halder
相关产品推荐
相关产品推荐

