<!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>