[英]click event of child not triggered
html : html :
<div id="schemeView" class="view" style="margin-top: 0px; opacity: 1; z-index: 1;">
<ul>
<li class="scheme-color" style="width:16.666666666666668%">
<div class="card" style="background:#288dff">
<span class="card-value">#288dff</span>
</div>
</li>
</ul>
</div>
CSS : CSS :
#schemeView {
position: absolute;
background: #f7f7f7;
top: 0;
z-index: -1;
opacity: 0;
white-space: nowrap;
}
#schemeView ul {
width: 100%;
height: 100%;
margin-top: 0;
padding: 0;
text-align: center;
}
.scheme-color {
height: 100%;
max-width: 400px;
list-style: none;
display: inline-block;
margin-top: 5%;
cursor: pointer;
}
div.card {
background: #288dff;
width: 90%;
height: 80%;
margin-left: 5%;]
}
span.card-value {
position: relative;
display: block;
width: 100%;
height: 35px;
background: rgba(243,243,243,.75);
text-align: center;
padding-top: 15px;
color: #555;
font-family: Monaco,Courier,monospace;
}
div.card:hover {
box-shadow: 0 20px 30px #777;
}
JS : JS :
$(document).ready(function() {
$('.scheme-color').click(function() {
alert('hi')
});
});
What it should do : 它应该做什么 :
li
element, it will alert 'hi'
li
元素时,都会提醒'hi'
What it's doing : 它在做什么 :
li
li
时绝对没有 .scheme-color
to #schemeView ul
it works perfectly but if i use #schemeView ul li
it does nothing .scheme-color
更改为#schemeView ul
它可以正常工作,但是如果我使用#schemeView ul li
则它什么也没做 I suspect it has something to do with the styling rules of the unordered list and its content
我怀疑它与无序列表的样式规则及其内容有关
Your code works fine. 您的代码工作正常。 But, if you're adding the target content after the page loads, use event delegation as follows:
但是,如果要在页面加载后添加目标内容,请按以下方式使用事件委托 :
$(document).ready(function() {
$('#schemeView').on('click', '.scheme-color', function() {
alert('hi')
});
});
$(document).ready(function() { $('#schemeView').on('click', '.scheme-color', function() { alert('hi') }); });
#schemeView { position: absolute; background: #f7f7f7; top: 0; z-index: -1; opacity: 0; white-space: nowrap; } #schemeView ul { width: 100%; height: 100%; margin-top: 0; padding: 0; text-align: center; } .scheme-color { height: 100%; max-width: 400px; list-style: none; display: inline-block; margin-top: 5%; cursor: pointer; } div.card { background: #288dff; width: 90%; height: 80%; margin-left: 5%;] } span.card-value { position: relative; display: block; width: 100%; height: 35px; background: rgba(243,243,243,.75); text-align: center; padding-top: 15px; color: #555; font-family: Monaco,Courier,monospace; } div.card:hover { box-shadow: 0 20px 30px #777; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="schemeView" class="view" style="margin-top: 0px; opacity: 1; z-index: 1;"> <ul> <li class="scheme-color" style="width:16.666666666666668%"> <div class="card" style="background:#288dff"> <span class="card-value">#288dff</span> </div> </li> </ul> </div>
It does work with $('.scheme-color')
. 它确实可以使用
$('.scheme-color')
。 Run the below snippet: 运行以下代码段:
$(document).ready(function() { $('.scheme-color').click(function() { alert('hi') }); });
#schemeView { position: absolute; background: #f7f7f7; top: 0; z-index: -1; opacity: 0; white-space: nowrap; } #schemeView ul { width: 100%; height: 100%; margin-top: 0; padding: 0; text-align: center; } .scheme-color { height: 100%; max-width: 400px; list-style: none; display: inline-block; margin-top: 5%; cursor: pointer; } div.card { background: #288dff; width: 90%; height: 80%; margin-left: 5%; ] } span.card-value { position: relative; display: block; width: 100%; height: 35px; background: rgba(243, 243, 243, .75); text-align: center; padding-top: 15px; color: #555; font-family: Monaco, Courier, monospace; } div.card:hover { box-shadow: 0 20px 30px #777; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div id="schemeView" class="view" style="margin-top: 0px; opacity: 1; z-index: 1;"> <ul> <li class="scheme-color" style="width:16.666666666666668%"> <div class="card" style="background:#288dff"> <span class="card-value">#288dff</span> </div> </li> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.