document.getElementById を $ などの別のものに変更する方法を知っている人はいますか?
次のように機能します。
$("theid")
これの代わりに:
document.getElementById("theid")
document.getElementById を $ などの別のものに変更する方法を知っている人はいますか?
次のように機能します。
$("theid")
これの代わりに:
document.getElementById("theid")
var $ = function(id){
return document.getElementById(id);
}
私はそれをアドバイスしませんが、document.querySelector(All)
より便利です。
function $(id){
return document.getElementById(id);
}
function $(id) {
return document.getElementById(id);
}
ドル記号は識別子として使用できる有効な文字なので、名前としてそれを使用して関数を宣言するだけです。
<html>
<head>
<script type="text/javascript">
function $(_i) {
return document.getElementById(_i);
}
</script>
</head>
<body>
<div id="myDiv">Hello</div>
<a href="javascript: alert($('myDiv').innerHTML)">Get InnerHTML</a>
</body>
</html>