[英]Hide/Show <div> according to URL in Prototype
我正在尝试使用原型框架来隐藏基于特定URL的'<div>'。 我无权访问服务器端-因此我别无选择,只能使用原型[使用原型限制平台]。
想知道是否有人可以告诉我如何在原型框架中做到这一点?
即我试图这样做但不起作用
Event.observe(window,'load',function(){
var url = document.location.href;
如果(url.indexOf('registered')> = 0){
$$('#side-menu-right')。hide(); }如果(url.indexOf('login')> = 0){$$('#side-menu-left')。hide(); }
});
爱一些帮助吗?
PS-从未使用过原型[jQuery man right here!]
我刚刚在JS Bin上做了一个测试用例,它抱怨:
Exception thrown: $$("#hello").hide is not a function
Caused by line (23/21): $$('#hello').hide();
(使用最新版本的原型)
使用时:
$('hello').style.display = "none";
它可以正常工作,请参见示例 。
编辑:我调整了JS Bin上的示例,以在涉及的元素到达之前有条件地向body
添加一个类名。 使用
.registered-user #hello { display: none; }
该元素根本不显示。 这不是最整洁的解决方案,因为您必须在文档中间添加一些script
,但是它可以工作。 如果有人知道更好的解决方案,请告诉。
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
.registered-user #hello { display: none; }
</style>
</head>
<body>
<script>
if (document.location.href.indexOf('registered')>=0)
$$('body')[0].addClassName('registered-user');
</script>
<p id="hello">Hello World</p>
</body>
</html>
更改
$$('#side-menu-right').hide();
至
$('side-menu-right').hide();
希望这可以帮助。
原因
1)如您所见,当Prototype是#side-menu-right
Javascript时,JQuery使用CSS表示法(因此对于具有ID的div
,没有#side-menu-right
)。
2) Struts
和Prototype
使用$
而不是 $$
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.