使用js操作selected元素选中某个option

2021-05-13 22:15:48
document.getElementsByClassName()[].setAttribute("selected","")选中元素,设置selected属性 ```html <!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> </head> <body> <select name="" id=""> <option class="shanghai" value="">上海</option> <option class="beijing" value="">北京</option> <option class="guangzhou" value="">广州</option> </select> <script> document.getElementsByClassName("guangzhou")[0].setAttribute("selected","") </script> </body> </html> ```