2017-08-03 63 views
-1

这并不严格地具有撤消按钮,但是我有灰色的功能并在点击时穿出物品。当再次点击时,我想让灰色和交叉线恢复正常(黑色而不是交叉)。 https://codepen.io/HelleFl/pen/OjNQop撤消功能不起作用

$(".item").click(function() { 
    $(this).css("text-decoration", "line-through"); 
    $(this).css("color", "gray");  
    if ($(this).css('color', 'gray') { 
    $(this).click(function() { 
     $(this).css('color', 'black'); 
     $(this).css('text-decoration', 'none'); 
     }) 
    }); 

我似乎无法弄清楚,并codepen告诉我检查了分号。

+2

不要这样做。不要在另一个绑定中嵌套绑定。相反,在顶级绑定中执行条件。同时使样式改变可以添加/删除的类大大减少了问题的范围。 – Taplar

+0

请问您能详细说明一下吗?因此,将函数内部的UNDO函数嵌套到DO中,而不是将IF函数完全分开? – Helle

回答

2

$(".item").click(function() { 
 
    $(this).toggleClass('grayStrike'); 
 
}); 
 

 

 
//or without a class 
 

 

 
$(".item2").click(function() { 
 
    if ($(this).css('text-decoration').split(' ')[0] !== "line-through") { 
 
    $(this).css("text-decoration", "line-through"); 
 
    $(this).css("color", "gray"); 
 
    } else { 
 
    $(this).css('color', ''); 
 
    $(this).css('text-decoration', ''); 
 
    } 
 
}); 
 

 

 
//delegate version so if new items are created, it doesn't matter 
 

 
$(document).on('click', ".item3", function() { 
 
    if ($(this).css('text-decoration').split(' ')[0] !== "line-through") { 
 
    $(this).css("text-decoration", "line-through"); 
 
    $(this).css("color", "gray"); 
 
    } else { 
 
    $(this).css('color', ''); 
 
    $(this).css('text-decoration', ''); 
 
    } 
 
});
.item.grayStrike { 
 
    color: gray; 
 
    text-decoration: line-through; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="item">test</div> 
 
<hr> 
 
<div class="item2">test1</div> 
 
<div class="item2">test2</div> 
 
<div class="item2">test3</div> 
 
<div class="item2">test4</div> 
 
<div class="item2">test5</div> 
 
<div class="item2">test6</div> 
 
<div class="item2">test7</div> 
 
<div class="item2">test8</div> 
 
<hr> 
 
<div class="item3">test1</div> 
 
<div class="item3">test2</div> 
 
<div class="item3">test3</div> 
 
<div class="item3">test4</div> 
 
<div class="item3">test5</div> 
 
<div class="item3">test6</div> 
 
<div class="item3">test7</div> 
 
<div class="item3">test8</div>

否则,如果你不想使用一个类。

+0

我想出了一个解决方案。感谢@lekens让我在那里。 – Helle

+0

@Helle增加了更多divs。对所有人都适用。 – Taplar

+0

@Hellie更新了代表版本。如果您是动态创建元素,那么新元素将不会绑定它们。所以你必须使用委托,所以它们在创建时并不重要,它应该可以工作。 – Taplar

0
$(".item").click(function() { 
     $(this).css("text-decoration", "line-through"); 
     $(this).css("color", "gray");  
     if ($(this).css('color', 'gray')) { 
     // $(this).click(function() { 
      $(this).css('color', 'black'); 
      $(this).css('text-decoration', 'none'); 
      }else{ 
      $(this).css('color', 'gray'); 
      $(this).css('text-decoration', 'line-through');} 
     // }) 
     }); 
+0

取出注释时,此代码无法正确执行。也许缺少(){}? – Helle

+0

一个错误...立即尝试 – Lekens

+0

这仍然不起作用。只需将您的代码复制并粘贴到我的代码簿中,然后亲自查看(和我哈哈)。似乎有一个问题与括号 – Helle

-1

A工作液:

$(document).on("click", ".item", function() { 
    if ($(this).css("text-decoration").split(" ")[0] !== "line-through") { 
    $(this).css("text-decoration", "line-through"); 
    $(this).css("color", "gray"); 
    } else { 
    $(this).css("color", ""); 
    $(this).css("text-decoration", ""); 
    } 
}); 
+0

再次,不要嵌套绑定。有了这个逻辑,每次点击2n + 1次,你就会在不需要的元素上添加另一个点击绑定。我更新了我的答案,以包含一个非类非双绑定版本。 – Taplar

+0

@Taplar这不起作用。点击时,它变成灰色。当再次点击时,没有任何反应 – Helle

+0

哪一个不工作? – Taplar