使用下面JavaScript方法都可以获取网易云音乐搜索框input元素
document.querySelector("input");
document.getElementById('srch');
在控制台执行也可以聚焦
window.setTimeout(function(){ document.getElementById("srch").focus();}, 6000);
设置快捷键在页面执行,报错Cannot read property 'focus' of null
document.addEventListener('keydown',function(event) {
var keynum = event.which;
//ALT+H
if(keynum==72 && event.altKey){
var searchBar = document.querySelector("input[name=wd],input[type=text],input[name=srch],input[autocomplete],input[type=search]")
if(searchBar !== null){
searchBar.focus();
}else {
document.getElementById('srch').focus();;
}
}
});
看上去是生效了,但是他里面是有iframe的。
所以得看你当前活跃的是那个iframe。有可能监听不到倒是真的