【问题标题】:jQuery get element by ID in Rails app not workingjQuery在Rails应用程序中按ID获取元素不起作用
【发布时间】:2012-10-21 01:19:12
【问题描述】:

我对此感到很困惑。我的 jQuery 代码在 .erb 页面上时有效,但在我的 rails 应用程序 instanokia.js 文件中时无效。谁能解释为什么这不起作用?我有一个可以正常工作并在 .ready 上弹出的警报,因此 jQuery 可以正常加载...

在我的 .erb 页面上:(有效)

    <div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <div id="experiment" value="toggle">
                <p>Hello</p>
            </div>
            <input type="button" id="toggleButton" value="toggle" class="btn btn-primary exp" />
        </div>
    </div>
<script>
    $('#toggleButton').click(function() {
        if ($('#experiment').is(':visible')) {
            $('#experiment').hide();
        } else {
            $('#experiment').show();
        }
    })
</script>

在我的 instanokia.js 文件中:(不起作用)

$(document).ready (

    alert('working!')

)

$('#toggleButton').click(function() {
    if ($('#experiment').is(':visible')) {
        $('#experiment').hide();
    } else {
        $('#experiment').show();
    }
})

【问题讨论】:

  • 另外,当我在 Chrome 中查看时,js 不会抛出任何错误...

标签: javascript jquery ruby-on-rails erb


【解决方案1】:

应该是的

$(document).ready(function(){

    alert('working!');

    $('#toggleButton').click(function() {
        if ($('#experiment').is(':visible')) {
            $('#experiment').hide();
        } else {
            $('#experiment').show();
        }
    })
});

【讨论】:

  • 是的,我以前做过类似的事情,但该功能仍然不存在,所以它抛出了错误。谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
相关资源
最近更新 更多