如何将这样的函数翻译成 jQuery?

发布于 2024-10-29 05:22:08 字数 297 浏览 2 评论 0原文

所以在我的代码中< /a> 我用过

function $(id) {
    return document.getElementById(id);
}

我想转移到 jQuery。怎么翻译这样的东西呢?

So in my code I used

function $(id) {
    return document.getElementById(id);
}

I want to move to jQuery. How to translate such thing to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

感情废物 2024-11-05 05:22:08

不要使用此函数,function $(id)。使用 jQuery 函数 $('#id')。这个函数将返回一个带有一堆 jQuery 方法的对象。

方法有remove()hide()toggle()等,实现如$('# id').hide()$('#id').show() 等。

jQuery 方法有很多很多,可以通过多种方式简化它。

Don't use this function, function $(id). Use the jQuery function $('#id'). This function will return an object with bunch of jQuery methods.

Methods include remove(), hide(), toggle(), etc., and the implementation is like $('#id').hide(), $('#id').show(), etc.

There are so many, many jQuery methods, that simplifies it in so many ways.

两仪 2024-11-05 05:22:08

如果我的问题正确,您将需要在页面中包含 jQuery,并将以下行:

return document.getElementById(id);

替换为以下行:

return $("# “+id);

If I got your question right, you will need to include jQuery inside your page, and replace the following line:

return document.getElementById(id);

with this one:

return $("#"+id);

瞄了个咪的 2024-11-05 05:22:08

使用 Jquery 选择器,您可以非常轻松地完成同样的事情。

$("#" + id)

有关更多详细信息:

http://api.jquery.com/id-选择器/

Using Jquery selectors you can do the same thing very easily.

$("#" + id)

For more details:

http://api.jquery.com/id-selector/

紫瑟鸿黎 2024-11-05 05:22:08

只需使用

$("#"+id)

That 就会返回具有正确 ID 的元素。

Just use

$("#"+id)

That will return the element with the right ID.

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