在 Rails 6 中测试 jquery [英] Testing jquery in Rails 6

查看:43
本文介绍了在 Rails 6 中测试 jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程和测试还很陌生,以查看 jquery 是否适用于我的 Rails 6 设置.为了做到这一点,我有:

  1. 创建了一个新的 rails 应用程序
  2. 生成脚手架
  3. 将以下代码插入到新创建的脚手架的 index.html.erb 视图中

<div id='mydiv' style='display:none'>测试

我希望这能显示 TEST 文本......但它似乎没有出现,即使页面的其余部分呈现......非常欢迎任何帮助......

解决方案

将脚本移到 div 下方,以便在执行脚本时加载 div:

 

<script type='text/javascript'>$('#mydiv').show();

或者你需要在页面加载时调用脚本:

$(document).ready(function(){$('#mydiv').show();});

I am fairly new to programming and testing to see if jquery works with my rails 6 setup. In order to do this I have:

  1. created a new rails application
  2. generated a scaffold
  3. inserted the following code into the index.html.erb view of the newly created scaffold

<script type='text/javascript'>
  $('#mydiv').show();
</script>

<div id='mydiv' style='display:none'>
   TEST
</div>

I would expect this to display the TEST text.. But it doesn't seem to appear even though the rest of the page renders... Any help very welcome...

解决方案

Move the script below the div, so that the div is loaded when the script is executed:

 <div id='mydiv' style='display:none'>
   TEST
 </div>
 <script type='text/javascript'>
   $('#mydiv').show();
 </script>

Or you need to call the script on page load:

$(document).ready(function(){
  $('#mydiv').show();
});

这篇关于在 Rails 6 中测试 jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆