Rails 中不引人注目的切换失败

发布于 2024-09-03 15:25:29 字数 2114 浏览 9 评论 0原文

我已经这样做了几个小时,但它就是不起作用。我正在尝试使用 Scriptaculous 的效果库让 ToggleJS 工作。具体来说,示例: http://wiseheartdesign.com/page_attachments/0000/0075/demo .html

我使用一个名为 new.html 的纯 html 文件让它工作得很好。然后我将其转换为 new.html.erb,这样我就可以将一些动画合并到我的 Rails 项目中,但它停止工作了!

我尝试了 html 和 ruby​​ javascript 标签,但都不起作用,例如:

<%= javascript_include_tag "application", "prototype", "effects", "lowpro", "toggle" %>

页面的正文显示在浏览器上,但没有动画。我已经确保我的 javascript 文件与演示相同(另外,html 文件无论如何都可以使用它们)。您知道我的 .erb 文件问题出在哪里吗?

我正在使用的 html 示例:

<h3>Toggle.LinkBehavior</h3> 
    <div class="expand_me" id="one">Expanded!</div> 
    <p> 
      <a class="toggle" href="#one" rel="toggle[one]">Expand</a> 
    </p> 
    <div class="expand_me" id="two">Expanded! 1</div> 
    <div class="expand_me" id="three">Expanded! 2</div> 
    <p> 
      <a class="toggle" href="#two" rel="toggle[two,three]">Expand even more</a> 
    </p> 

#Application.js
Event.addBehavior({
   'a.toggle': Toggle.LinkBehavior(),
   'a.inverted_toggle': Toggle.LinkBehavior({invert: true}),
   'a.open': Toggle.LinkBehavior({
  effect: 'blind',
  onLoad: function(link) {
    this.toggle();
  },
  beforeToggle: function(link) {
    link.innerHTML = (link.innerHTML == "Open") ? "Opening..." : "Closing...";
  },
  afterToggle: function(link) {
    link.innerHTML = (link.innerHTML == "Opening...") ? "Close" : "Open";
  }
   }),
   'form input.checkbox.toggle': Toggle.CheckboxBehavior(),
   'form input.checkbox.inverted_toggle': Toggle.CheckboxBehavior({invert: true}),
   'form .radio_group.toggle': Toggle.RadioGroupBehavior({effect: 'blind'}),
   'form select.toggle': Toggle.SelectBehavior()
 });

Eimantas,我对此完全是新手。我在我的 html 文档中添加了以下内容,但它不起作用:

<script type="text/javascript">
window.onload = Event.addBehavior
</script>

这就是您的意思吗?或者您能告诉我需要在 .html.erb 文件中编写哪些代码吗?

I've been at this for hours but it just isn't working. I'm trying to get ToggleJS working using Scriptaculous' Effects library. Specifically, the example: http://wiseheartdesign.com/page_attachments/0000/0075/demo.html.

I got it working beautifully using a pure html file called new.html. I then converted it to new.html.erb so I can incorporate some of the animations into my Rails project and it stopped working!

I tried both html and ruby javascript tags but neither are working, for example:

<%= javascript_include_tag "application", "prototype", "effects", "lowpro", "toggle" %>

The page's body is displaying on the browser, but without animations. I have made sure my javascript files are the same as the demo (plus, the html file is working with them anyway). Do you know where the problem might lie here with my .erb file?

A sample of the html I'm using:

<h3>Toggle.LinkBehavior</h3> 
    <div class="expand_me" id="one">Expanded!</div> 
    <p> 
      <a class="toggle" href="#one" rel="toggle[one]">Expand</a> 
    </p> 
    <div class="expand_me" id="two">Expanded! 1</div> 
    <div class="expand_me" id="three">Expanded! 2</div> 
    <p> 
      <a class="toggle" href="#two" rel="toggle[two,three]">Expand even more</a> 
    </p> 

#Application.js
Event.addBehavior({
   'a.toggle': Toggle.LinkBehavior(),
   'a.inverted_toggle': Toggle.LinkBehavior({invert: true}),
   'a.open': Toggle.LinkBehavior({
  effect: 'blind',
  onLoad: function(link) {
    this.toggle();
  },
  beforeToggle: function(link) {
    link.innerHTML = (link.innerHTML == "Open") ? "Opening..." : "Closing...";
  },
  afterToggle: function(link) {
    link.innerHTML = (link.innerHTML == "Opening...") ? "Close" : "Open";
  }
   }),
   'form input.checkbox.toggle': Toggle.CheckboxBehavior(),
   'form input.checkbox.inverted_toggle': Toggle.CheckboxBehavior({invert: true}),
   'form .radio_group.toggle': Toggle.RadioGroupBehavior({effect: 'blind'}),
   'form select.toggle': Toggle.SelectBehavior()
 });

Eimantas, I am a total newbie at this. I added to my html doc the following but it didn't work:

<script type="text/javascript">
window.onload = Event.addBehavior
</script>

Is this what you mean, or can you tell me what code I need to write in my .html.erb file?

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

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

发布评论

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

评论(1

东京女 2024-09-10 15:25:29

你能粘贴为 javascript 文件生成的 html 吗?

我认为您的 toggle.js 加载时间晚于 application.js 文件,因此如果 application.js 具有所有挂钩代码 (window.onload...) 用于切换效果,它将失败,因为库本身尚未加载。

更新:

以下是如何将 Event.addBehavior 放入窗口加载:

<script type="text/javascripts">
  window.onload = function() {
    Event.addBehavior({
      // all params and code.
    });
  }
</script>

您应该检查有关执行原型样式 window.onload 函数的原型文档。

Can you paste generated html for javascript files?

I think your toggle.js is loaded later than application.js file, hence if application.js has all the hook code (window.onload...) for toggling effect it will fail since the library itself isn't loaded yet.

UPDATE:

Here's how you put Event.addBehavior to window loading:

<script type="text/javascripts">
  window.onload = function() {
    Event.addBehavior({
      // all params and code.
    });
  }
</script>

You should check prototype documentation on doing prototype style window.onload function.

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