我试图删除课程' active'当你第二次点击复选框时,就像用户添加一个图钉时Pinterest为Twitter / Facebook复选框所做的那样:
添加'有效'点击课很容易。但是,我无法确定添加后如何删除它。我试过这个,但它没有工作:
$(".add_link_twitter.active").click(function(e) {
$(this).removeClass(activePostTwitter);
});
我有两个问题:
提前致谢!
这里是jQuery:
var postTwitter = ".add_link_twitter";
var activePostTwitter = "active";
$(postTwitter).click(function(e) {
$(this).addClass(activePostTwitter);
});
这是html:
<label class="add_link_twitter">
<input type="checkbox" name="publish_to_twitter" class="publish_to_twitter"><span>Share on Twitter</span>
</label>
这是css:
.add_link_twitter{
position:absolute;
left:15px;
bottom:16px;
color: #a19486;
border: 2px solid transparent;
border-color: #F0EDE8;
border-radius: 4px;
font-size: 13px;
font-weight: bold;
cursor: pointer;
padding: 7px;
padding-top: 5px;
padding-bottom: 5px;
}
.active {
border-color: #468BD0;
color: #468BD0;
background-color: whiteSmoke;
}
.add_link_twitter:hover
{
color: #A19486;
border: 2px solid transparent;
border-color: #C2B1A2;
border-radius: 4px;
background-color: white;
padding: 7px;
padding-top: 5px;
padding-bottom: 5px;
}
答案 0 :(得分:8)
而不是
$(postTwitter).click(function(e) {
$(this).addClass(activePostTwitter);
});
使用
$(postTwitter).click(function(e) {
$(this).toggleClass(activePostTwitter);
});
编辑:
该事件每次点击触发两次,可能是因为事件传播。要解决此问题,请将处理程序分配给input
并让它更改其父级的类:
$(postTwitter + " input").click(function(e) {
$(this).parent().toggleClass(activePostTwitter);
});
确认jsfiddle:http://jsfiddle.net/bpfqB/
答案 1 :(得分:1)
这适用于您的两个问题:
$(function() {
"use strict";
var $postTwitter = $("label.add_link_twitter");
$postTwitter.find("input:checkbox").click(function() {
$(this).parent().toggleClass("active");
if($("input.publish_to_twitter").is(":checked")) {
$(this).parent().removeClass("hover");
}
});
$postTwitter.hover(
function() {
if($("input.publish_to_twitter").is(":checked")) {
return;
}
$(this).addClass("hover");
},
function() {
$(this).removeClass("hover");
});
});
你需要对CSS进行一些更改,你必须使用jQuery进行悬停(跳过CSS悬停)。