Rails jQuery 手风琴

发布于 2024-09-26 03:13:40 字数 588 浏览 4 评论 0原文

如何设置 jQuery 来与 Rails 一起使用。我特别想使用手风琴功能。我之前在 PHP 中使用过这个,但我无法弄清楚 Rails 应用程序中的所有内容都需要放在哪里。我已将 jQuery 导入到 javascript 文件夹中并将其包含在应用程序中。我的 HTML 布局正确,但我不明白以下内容应该去哪里:

<script>
  $(document).ready(function() {
    $("#accordion").accordion();
  });
</script>

我也尝试安装 jRails 并使用 scriptaculous Accordion,但它们都不起作用,所以我认为我缺少 javascript 和导轨。

我已经仔细寻找了一些关于如何设置它的明确说明,如果有人有链接或可以引导我完成它,我将不胜感激。

检查 firebug 后,我可以看到出现以下错误:

$("#accordion").accordion is not a function

I call jQuery before I call application.js

How do you set up jQuery to work with rails. I specifically want to use the accordion feature. I have used this before with PHP but i cannot work out where everything needs to go in a rails application. I have imported jQuery into the javascript folder and included it in the application. My HTML is laid out correctly, but i don't understand where the following should go:

<script>
  $(document).ready(function() {
    $("#accordion").accordion();
  });
</script>

I have also tried installing jRails and using the scriptaculous Accordion, but neither of them worked either, so I think I am missing some vital link between javascript and rails.

I have looked all over for some explicit instructions on how to set it up, if anyone has a link or can walk me through it it would be appreciated.

After checking firebug I can see I am getting the following error:

$("#accordion").accordion is not a function

I call jQuery before I call application.js

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

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

发布评论

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

评论(3

血之狂魔 2024-10-03 03:13:41

如果你不想要 UJS 系统,你只需要把这一行放在你的视图中。

相反,您可以将其放在 application.js 上并在头节点中需要它。

You just need put this line in your view if you don't want UJS system.

Instead you can put it on your application.js and require it in your head node.

傾城如夢未必闌珊 2024-10-03 03:13:41

您还需要确保注释掉默认的 javascript 库,因为开箱即用的 Rails 应用程序附带原型,这将与您的 jQuery 库冲突。

它看起来像这样:

<%= javascript_include_tag :defaults %>

您在 firebug 中遇到任何 javascript 错误吗?

You also need to make sure that you commented out default javascript libraries, because out of the box, Rails applications come packaged with prototype which will conflict with your jQuery library.

It looks like this :

<%= javascript_include_tag :defaults %>

Are you hitting any javascript errors in firebug?

断桥再见 2024-10-03 03:13:41
  1. 我下载了错误的 jQuery 库,网站上的链接指向不完整的 .js 文件。从以下位置下载您需要的内容: http://jqueryui.com/download 不要只从 http://docs.jquery.com/Downloading_jQuery
  2. 仅在标准布局中直接调用您需要的 javascript 文件,执行不使用 :defaults 选项。
  3. 如果您要在不同页面上使用很多奇特的 jQuery 东西,请将文档就绪函数添加到您的视图中,否则将其添加到您的 application.js
  4. 确保在 application.js 文件之前调用 jQuery UI 文件,否则它不会按正确的顺序找到函数。
  1. I downloaded the wrong jQuery library, the link on the website pointed to an incomplete .js file. Download what you need from: http://jqueryui.com/download don't just download the file from http://docs.jquery.com/Downloading_jQuery
  2. Only call the javascript files you need directly in your standard layout, do not use the :defaults option.
  3. If you are going to use a lot of fancy jQuery things on different pages add the document ready function to your view, else add it to your application.js
  4. Ensure that you call the jQuery UI files before the application.js file or it will not find the functions in the right order.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文