我可以在页面加载时将 link 格式化为 show/hide 某些元素吗?
Can I format a link to show/hide certain elements upon page load?
我有一个页面,其中的元素可以 shown/hidden 与 jquery:
<div id="hide">HIDE</div>
<div id="show">SHOW</div>
<script type="text/javascript">
$("#show").hide();
$("#hide").hide();
$("#hide").click(function () {
$(this).hide();
$("#show").show();
});
$("#show").click(function () {
$(this).hide();
$("#hide").show();
});
</script>
路线:
get "static/showhide" => "static#showhide", :as => :showhide_static
因此,当我 link 使用 showhide_static_path
访问页面时,页面加载时 div 都隐藏了。有没有一种方法可以根据我写的 link 来控制哪些 div 被隐藏,哪些被显示?
我的一个想法是做这样的事情:
<% if @x == true %>
<script type="text/javascript">
$("#show").hide();
</script>
<% elsif @y == true %>
<script type="text/javascript">
$("#show").hide();
</script>
<% end %>
<script type="text/javascript">
$("#hide").click(function () {
$(this).hide();
$("#show").show();
});
$("#show").click(function () {
$(this).hide();
$("#hide").show();
});
</script>
有没有一种方法可以根据 link 的语法设置 @x
或 @y
的值?
您可以像这样通过查询字符串传递参数:example.com/static/showhide?x=1&y=0
。然后您可以使用参数哈希访问这些,例如:params[:x]
.
另一种方法是使用纯粹的 javascript 和 url 片段。例如:example.com/static/showhide#1
。要访问此表单 javascript:window.location.hash
http://en.wikipedia.org/wiki/Fragment_identifier
奖金提示:
- 你不需要做
<% if @x == true %>
你可以只做 <% if @x %>
。
- 你可能想给你的变量起更有意义的名字。
我有一个页面,其中的元素可以 shown/hidden 与 jquery:
<div id="hide">HIDE</div>
<div id="show">SHOW</div>
<script type="text/javascript">
$("#show").hide();
$("#hide").hide();
$("#hide").click(function () {
$(this).hide();
$("#show").show();
});
$("#show").click(function () {
$(this).hide();
$("#hide").show();
});
</script>
路线:
get "static/showhide" => "static#showhide", :as => :showhide_static
因此,当我 link 使用 showhide_static_path
访问页面时,页面加载时 div 都隐藏了。有没有一种方法可以根据我写的 link 来控制哪些 div 被隐藏,哪些被显示?
我的一个想法是做这样的事情:
<% if @x == true %>
<script type="text/javascript">
$("#show").hide();
</script>
<% elsif @y == true %>
<script type="text/javascript">
$("#show").hide();
</script>
<% end %>
<script type="text/javascript">
$("#hide").click(function () {
$(this).hide();
$("#show").show();
});
$("#show").click(function () {
$(this).hide();
$("#hide").show();
});
</script>
有没有一种方法可以根据 link 的语法设置 @x
或 @y
的值?
您可以像这样通过查询字符串传递参数:example.com/static/showhide?x=1&y=0
。然后您可以使用参数哈希访问这些,例如:params[:x]
.
另一种方法是使用纯粹的 javascript 和 url 片段。例如:example.com/static/showhide#1
。要访问此表单 javascript:window.location.hash
http://en.wikipedia.org/wiki/Fragment_identifier
奖金提示:
- 你不需要做
<% if @x == true %>
你可以只做<% if @x %>
。 - 你可能想给你的变量起更有意义的名字。