简体   繁体   English

点击不在div元素上注册的事件(jQuery / HTML / CSS)

[英]Click event not registering on div element (jQuery/HTML/CSS)

This is my game: (i can't post images yet so I have to explain with words) This is a Connect 4 game. 这是我的游戏:(我无法发布图片,所以我必须用文字解释)这是一个Connect 4游戏。 Imagine 7 div columns. 想象一下7个div列。 The 7 columns have 6 div chip objects stacked in each column element. 7列在每个列元素中堆叠有6个div芯片对象。 (7x6 grid with 42 chips) (7x6网格,42芯片)

  • I put each chip (each black circle) as a div object prepended into each column container. 我将每个芯片(每个黑色圆圈)作为div对象放在每个列容器中。
  • that is... each .columncontainer div has nested under it a bunch of .chip div elements. 那就是......每个.columncontainer div都嵌套了一堆.chip div元素。 (they're the chips/circles) (他们是筹码/圈子)

What I want: I want to be able to click a specific chip object (they're div's.. the black circles) and I want ONLY that one to turn full black. 我想要的是:我希望能够点击一个特定的芯片对象(它们是div的黑色圆圈)而且我只想要一个全黑的。 (default opacity I set to 0.5) (默认不透明度我设置为0.5)

This is the code I have: 这是我的代码:

$(document).ready(function colorSelectionListener(){
$(".columncontainer").children().click(function() {
    window.alert("clicked!");
    $(this).css("opacity",1);
});

What's actually happening: When I click on ANY of the chip objects... NOTHING happens. 实际发生了什么:当我点击任何芯片对象时......没有发生。 I can't click the chips at all. 我根本无法点击芯片。

What I can do: To test if I can click something I made the alert "clicked!" 我能做什么:测试我是否可以点击我点击的内容“点击!” In this way I am able to click the columncontainers. 通过这种方式,我可以单击columncontainers。 I am also able to retrieve the index of the column div inside it when I print out the index of 'this', like so: 当我打印出'this'的索引时,我也能够检索其中div列的索引,如下所示:

window.alert($(this).index());

instead of the 'clicked!' 而不是'点击!' message. 信息。 It gives me 6...which doesn't make sense... because 6 is the last element inside columncontainer which is .column. 它给了我6 ...这没有意义......因为6是columncontainer中的最后一个元素是.column。 (0-5 elements must be the chips after I prepend them right?) (在我将它们添加到正确之后,0-5元素必须是芯片?)

What I tried: I tried making the .click with the chip objects themselves. 我尝试了什么:我尝试用芯片对象自己制作.click。 (the class attached to every chip object is '.chip') Did not work. (附加到每个芯片对象的类是'.chip')没有用。 (Click was not registering... but I think that's another problem) (点击没有注册......但我认为这是另一个问题)

Can someone enlighten me? 有人可以开导我吗?

EDIT 1: Mini Recreation of Problem https://jsfiddle.net/9z916z2u/65/ 编辑1: 问题的微型重建 https://jsfiddle.net/9z916z2u/65/

If anyone could help me I would really really appreciate it! 如果有人能帮助我,我真的很感激! I'm having so much fun coding this right now but this is annoying :/ I learnt jQuery/Javascript around 3 days ago, so I'm not that good. 我现在正在编写这么多的乐趣,但这很烦人:/我在3天前学习了jQuery / Javascript,所以我不是那么好。 (I have coded in Java/Python before though) (我以前用Java / Python编写过)

$(document).ready(function(){

    $(".columncontainer > childElement").click(function() {
        window.alert("clicked!");
        $(this).css("opacity",1);
     }    
});

replace child element with d tag or the class of the childred of .columncontainer 用d标记或.columncontainer的childred类替换子元素

Try this: 尝试这个:

$('.columncontainer .cheap').click(function(){
    $(this).css('opacity', 1)
})

Or, if you want to add .cheap blocks dynamically, you can use this variant: 或者,如果要动态添加.cheap块,可以使用以下变体:

$('.columncontainer').on('click', '.cheap', function(){
    $(this).css('opacity', 1)
})

Posted code in JSFiddle needs some adjustments to work. 在JSFiddle中发布的代码需要进行一些调整才能工作。 I've modified the event subscription to match late binding (according to Anatoliy Arkhipov). 我修改了事件订阅以匹配后期绑定(根据Anatoliy Arkhipov)。

$('.columncontainer').on('click', '.cheap', function(){

Instead of 代替

$('.cheap').on('click', function(){

And removed position: relative for the .column class. 并删除position: relative对于.column类。 Here is how the code looks now and it works in Chrome and FF. 以下是代码的外观,它适用于Chrome和FF。

However, I consider the answer to be not consistent (why does position: relative prevented binding?) and maybe masters of css can explain that. 但是,我认为答案是不一致的(为什么position: relative阻止绑定?)并且css的主人可以解释这一点。

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

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