简体   繁体   English

jQuery:单击更改CSS选择器

[英]Jquery: change css selector on click

I just get {"error": "Please use POST request"} when I run this 运行此命令时,我刚收到{“ error”:“请使用POST请求”}

$("#ricomporreclick").click(function () {
    $("#film").css('visibility', 'hidden');
    $("#ricomporre").css('visibility', 'visible');
});

What's wrong with the code? 代码有什么问题? I'm tring to change the selector without page being reloaded.. when the click is triggered #film should be display:none and #ricomporre should be visible. 我正在尝试在不重新加载页面的情况下更改选择器..触发单击时,应该显示#film:none和#ricomporre应该是可见的。

http://jsfiddle.net/8Ndba/ http://jsfiddle.net/8Ndba/

just put return false at end of the code block. 只需将return false放在代码块的末尾。

Updated Your Fiddle 更新了您的小提琴

Try to use event.preventDefault() to prevent the default functionality of the anchor tag, 尝试使用event.preventDefault()来阻止锚标记的默认功能,

$("#ricomporreclick").click(function (e) {
    e.preventDefault()
    $("#film").css('visibility', 'hidden');
    $("#ricomporre").css('visibility', 'visible');
});

DEMO 演示

Change your anchor so the URL doesn't do anything: 更改锚点,以使URL不执行任何操作:

<a href="javascript:void()" id="ricomporreclick">HERE</a> 

DEMO 演示

You need to stop the link from redirecting by using the preventDefault method. 您需要使用preventDefault方法停止链接的重定向。 I've edited the fiddle to show this 我已经编辑了小提琴以显示此内容

$("#ricomporreclick").click(function (e) {
    $("#film").css('visibility', 'hidden');
    $("#ricomporre").css('visibility', 'visible');
    e.preventDefault();
});

What was happening previously was that it was correctly changing the visibility, but then the link was reloading the page causing the css to be reset to the stylesheet. 以前发生的事情是它正确地更改了可见性,但是随后该链接重新加载了页面,从而导致css重置为样式表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM