如何在div的特定x和y位置上触发点击事件?

时间:2019-05-05 10:20:36

标签: javascript jquery eventtrigger

通过此代码,我不知道如何触发坐标 (X = 1043, Y = 73)在人体负荷上保持坐标。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

<div class="rating"></div>

<div id="status"> </div>

<script>

$(document).ready(function(){
$(".rating").click(function(){
  // X = 1043 
  // Y = 73
  var x = event.clientX;
  var y = event.clientY;
  var coords = "X coords: " + x + ", Y coords: " + y;
  document.getElementById("status").innerHTML = coords;
}); 
});

</script>

2 个答案:

答案 0 :(得分:1)

您可以简单地触发click事件,例如:

$(".rating").click(function(){
 // your code...
}).click() // triggers on first load/ready

或者,

.trigger('click')

答案 1 :(得分:0)

$(function() {
  $('.rating').on('page:click', function(e) {
  var coords = "X coords: " + e.clientX + ", Y coords: " + e.clientY,
  aim = document.elementFromPoint(e.clientX, e.clientY);
  $(aim).click();
  console.log(aim);
  $('#status').text(coords);
  });
  var action = $.Event('page:click', {clientX: 1043, clientY: 70});
  $('.rating').trigger(action);
});