<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #my-button:focus-within { color: red; } </style> </head> <body> <button id="my-button" tabindex="0">item2</button> </body> </html>
按下後,顏色應切換為紅色。在 Chrome 和 Firefox 中工作,Safari(在版本 15.6.1 上測試)總是會出現問題。
但是問題是什麼?
P粉1419112442023-09-15 13:05:45
似乎 :focus-within 無法應用於 Safari 中的按鈕。但這是有效的:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #my-li:focus-within > #my-button { color: red; } </style> </head> <body> <ul> <li id="my-li" tabindex="0"> <button id="my-button">item2</button> </li> </ul> </body> </html>