console
(function () {
$(() => {
$(document).on("click", "label[for]", e => {
const selected = $(e.target).attr("for");
if (document.getElementById(selected)) {
return;
}
try {
const jq = eval(`(function(e){ return e.${selected} })`)($(e.target));
if (jq.length > 0) {
jq.eq(0).focus();
}
} catch (e) {
}
try {
const jq = $(selected);
if (jq.length > 0) {
jq.eq(0).focus();
}
} catch (e) {
}
});
});
})();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="">
<title></title>
</head>
<body>
<div>
<div>
<label for="input1">点我</label><br/>
<input id="input1" type="text" name="name" value="" />
</div>
<label for="input1">点我</label>
<input id="input1" type="text" name="name" value="" />
</div>
<hr />
<div>
<div>
<label for=".input2">点我</label><br/>
<input class="input2 type="text" name="name" value="" />
</div>
<label for=".input2">点我</label>
<input class="input2" type="text" name="name" value="" />
</div>
<hr />
<div>
<div>
<label for="nextAll(':input:first')">点我</label><br/>
<span></span><br/>
<input type="text" name="name" value="" />
</div>
<label for="nextAll(':input:first')">点我</label>
<span></span>
<input type="text" name="name" value="" />
</div>
</body>
</html>