我需要注册一个click事件,当我点击(“$ p2”)元素时需要调用同一个事件
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function(){
$("#p1").on( 'click', function( event) {
console.log( 'clicked = ', $(this).text() );
$("#p2").on( 'click', function( event ) {
$("p").trigger( 'click' );
})
})
});
</script>
<style>
</style>
</head>
<body>
This is a scratch html.
<br>
<p id="p1">This is paragraph - p1</p>
<p id="p2">This is paragraph - p2</p>
</body>
</table>
</html>
答案 0 :(得分:0)
我认为你要找的是多选择器
$(document).ready(function () {
$("#p1, #p2").on('click', function (event) {
console.log('clicked = ', $(this).text());
})
});
演示:Fiddle