我有一些HTML:
<html>
<body>
<div id="parent">
<input id="child1" onclick="clickedInline();" style="z-index:999" disabled readonly>Child One</input>
<input id="child2">Child Two</input>
</div>
</body>
</html>
还有一些像这样的jquery:
// Wait for page to load
$(function(){
// Log clicked item
$('*').click(function(){
console.log($(this).prop("tagName")+'#'+$(this).attr('id'));
});
// I want this to happen
$('#child1').click(function(){
alert("It worked!");
});
// I've tried this too
$('#child1').on("click", function(){
alert("It worked!");
});
// And even this
$('#parent').on("click", '#child1', function (){
alert("It worked!");
});
});
// This doesn't work either
function clickedInline(){
alert("It worked!");
}
当我点击#child1
时,不会弹出任何警告,并记录下来:
DIV#parent
BODY#undefined
HTML#undefined
.click()
事件似乎只在父元素上触发 ,而不是子元素。单击#child2
时,我不希望它触发。
答案 0 :(得分:1)
您的代码似乎与警告框一起使用,您似乎确实以错误的方式关闭了您的结束主体和html标签。
// Wait for page to load
$(function(){
// Log clicked item
$('*').click(function(){
console.log($(this).prop("tagName")+'#'+$(this).attr('id'));
});
// I want this to happen
$('#child1').click(function(){
alert("It worked!");
});
// I've tried this too
$('#child1').on("click", function(){
alert("It worked!");
});
// And even this
$('#parent').on("click", '#child1', function (){
alert("It worked!");
});
});
// This doesn't work either
function clickedInline(){
alert("It worked!");
}
&#13;
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
</head>
<body>
<div id="parent">
<input id="child1" onclick="clickedInline();" style="z-index:999">Child One</input>
<input id="child2">Child Two</input>
</div>
</body>
</html>
&#13;
答案 1 :(得分:0)
我明白了......如果<input>
标记具有disabled
属性,则所有点击事件都不起作用。