函数 elm(obj){ return document.getElementById(obj); }

发布于 2024-12-18 06:46:05 字数 265 浏览 1 评论 0原文

我使用以下函数来简化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲸落 2024-12-25 06:46:05

你的功能没有任何问题。

唯一的问题是,如果您像许多使用 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.

怀念你的温柔 2024-12-25 06:46:05

是的,你可以使用它。如果您使用 jQuery,则存在更简单的方法,例如:

$(document).ready(function() {
  $("#yourElementId").html(); // or any other function you want instead of html
});

希望有帮助

Yes you can use that. If you are using jQuery then easier way exist, like:

$(document).ready(function() {
  $("#yourElementId").html(); // or any other function you want instead of html
});

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文