在 Javascript 中更改 document.getElementById
Change document.getElementById in Javascript
>有谁知道如何将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>
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何用更合适的内容替换document.write
- document.styleSheets不返回任何内容
- jQuery document.ready not working
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- reactjs this.refs vs document.getElementById
- 如何通过adf中的document.getElementById获取inputText字段值
- window.opener.document在ie中不起作用
- contentWindow.document.body is null
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- document.getElementById(“st”).click();不起作用
- $(document).height和$(window).heaght都返回相同的值-使用的是正确的doctype
- jQuery document.ready停止代码
- document.getElementById并使用id名称
- 使用jQuery 1.8.1准备好多个$(document).是否有任何开销
- document.getElementById在js中不起作用
- document.getElementById.style.backgroundImage not working
- 停止对document.ready函数的重定向/刷新
- 是否可以 document.getElementsByTagName('head')[0] ever 返回 null