简体   繁体   English

Div Click Event Not Working - jQuery

[英]Div Click Event Not Working - jQuery

I've looked at solutions in several other threads (most with solutions similar to this one: div click event not working ) and am just not having any luck getting click events to work on my div. 我已经在其他几个线程中查看了解决方案(大多数解决方案与此类似: div click事件无法正常工作 )并且我没有运气获得点击事件来处理我的div。 See js code below: 请参阅下面的js代码:

$(document).ready(function () {
    $(document).on('click', '#l1', function() {
        alert('clicked header');
    });
});

Html: HTML:

<div id = 'main' class = 'container' style = 'height: 100%; width: 100%; float: left;'>
  <div id = 'left' class = 'container' style = 'height: 100%; width: 70%; float: left;'>
    <div id = 'l1' class = 'container bordered' align = 'right' style = 'height: 100%; width: 20%; float: left;'>
      <h1 id = 'l1Header'>Div Text</h1>
    </div>
  </div>
</div>

Css: CSS:

html body {
  height: 100%;
  width: 100%;
}

.container {
  position: relative;
  z-index: -10;
}

.bordered {
  border: 2px solid #808080;
}

#main {
  font-size: 12px;
  text-align: right;
  font-family: sans-serif;
  line-height: 1.5;
  vertical-align: middle;
}

h1 {
  font-size: 18px;
  text-align: center;
  font-family: sans-serif;
  font-weight: normal;
  line-height: 1.5;
  vertical-align: top;
  z-index: 10;
}

Any ideas? 有任何想法吗?

EDIT: It was a result of the z-index. 编辑:这是z-index的结果。 Later on in the code was an svg being added on top of the div making it impossible to interact with. 后来代码中有一个svg被添加到div之上,因此无法与之交互。 Thank you all! 谢谢你们!

I'm not really sure why you're calling the document as opposed to just adding the event to the element. 我不确定你为什么要调用文档而不是仅仅将事件添加到元素中。 I'm also not really sure if you're trying to use jQuery or JS here. 我也不确定你是否在尝试使用jQuery或JS。 You say JS, but the function you are using is jQuery and will only work if you're using the jQuery library (not trying to be demeaning if you know better). 你说JS,但你正在使用的函数是jQuery,只有在你使用jQuery库时才会起作用(如果你知道的话,不要试图去贬低)。 Give this function a try: 试试这个功能:

document.getElementById("l1").addEventListener("click", function(){
alert("Clicked Header");

}); });

it could also have to do with your z-indexes. 它也可能与您的z索引有关。 Not really sure why they're necessary based on just this snippet of code. 基于这段代码,不确定为什么它们是必要的。

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

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