函数 elm(obj){ return document.getElementById(obj); }
我使用以下函数来简化 getElementById 的使用:
function elm(obj){
return document.getElementById(obj);
}
//Usage: elm("id").innerHTML = "hello";
我的问题:
- 是否建议使用它?
- 有更好的方法来实现这一目标吗?
I am using the following function to simplify the use of getElementById:
function elm(obj){
return document.getElementById(obj);
}
//Usage: elm("id").innerHTML = "hello";
My Questions:
- Is it recommended to use it?
- Are there better ways of achieving this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的功能没有任何问题。
唯一的问题是,如果您像许多使用 Javascript 框架来帮助处理事件或 AJAX 等真正烦人的事情的人之一,那么使用您的库已经提供的任何函数来选择 DOM 元素可能更惯用。
There is nothing wrong with your function.
The only problem is that if you are like one of the many people that use a Javascript framework to help with the really annoying stuff like events or AJAX then it is probably more idiomatic to instead use whatever function your library already provides to select DOM elements.
是的,你可以使用它。如果您使用 jQuery,则存在更简单的方法,例如:
希望有帮助
Yes you can use that. If you are using jQuery then easier way exist, like:
Hope it helps