gpt4 book ai didi

jquery "cannot read propery ' id' of null"when tracking event.pageX+Y

转载 作者:行者123 更新时间:2023-11-28 09:18:24 25 4
gpt4 key购买 nike

我在创建跟踪用户鼠标指针并在单击时与 div 元素交互的 Web 小程序时遇到问题。

工作正常,直到我在小程序上方添加一个新部分。当我尝试点击任何可定位 div 的应用程序部分内的任何内容时,我得到:

Uncaught TypeError: Cannot read property 'id' of null demo.js:27

(anonymous function) demo.js:27

x.event.dispatch jquery.min.js:5

v.handle jquery.min.js:5

当我从页面顶部删除新部分时,一切正常。

http://jsfiddle.net/gv8qv43b/

This fiddle 重现了错误,但无论是否有附加部分,它都是如此。这也很奇怪。

代码:

$(document).ready(function () {
$("#phone").css('display', 'none');

$("#outer").mouseenter(function () {
$("#phone").fadeIn('fast', 0);
});

$("#outer").mousemove(function (event) {

var parentOffset = $("#outer").offset();
var relX = event.pageX - parentOffset.left;
var relY = event.pageY - parentOffset.top;

var phone = $("#phone");

relX = relX - (phone.width() / 2);
relY = relY - (phone.height() / 2);

phone.css("left", relX + 'px');
phone.css("top", relY + 'px');
});

$("#phone").click(function (event) {
var phone = $("#phone");
phone.css('display', 'none');
var elem = document.elementFromPoint(event.pageX, event.pageY);
console.log('Clicked: ' + elem.id);
phone.css('display', 'block');
});

$("#outer").mouseleave(function () {
$('#phone').fadeOut('fast', 0);
});
});
#top-bg {
position: relative;
width: 100%;
height: 700px;
background: center center no-repeat;
background-color: #000000;
overflow: hidden;
}
#top-bg .landingtext {
width: 380px;
position: absolute;
top: 30%;
right: 15%;
}
#outer {
position: relative;
width: 100%;
height: 700px;
background: center center no-repeat;
background-color: #eff0f2;
overflow: hidden;
}
#thing1 {
left: 50%;
top: 112px;
margin-left: 280px;
position: absolute;
width: 125px;
height: 150px;
background-color: green;
}
#thing2 {
left: 50%;
top: 265px;
margin-left: 195px;
position: absolute;
width: 120px;
height: 140px;
background-color: blue;
}
#thing3 {
left: 50%;
top: 410px;
margin-left: 35px;
position: absolute;
width: 252px;
height: 85px;
background-color: yellow;
}
#thing4 {
left: 50%;
top: 475px;
margin-left: 220px;
position: absolute;
width: 155px;
height: 150px;
background-color: red;
}
#thing5 {
right: 50%;
top: 400px;
margin-right: 20px;
position: absolute;
width: 110px;
height: 205px;
background-color: cyan;
}
#thing6 {
right: 50%;
top: 400px;
margin-right: 140px;
position: absolute;
width: 112px;
height: 205px;
background-color: grey;
}
#thing7 {
right: 50%;
top: 400px;
margin-right: 270px;
position: absolute;
width: 112px;
height: 205px;
background-color: purple;
}
#thing8 {
right: 50%;
top: 397px;
margin-right: 400px;
position: absolute;
width: 112px;
height: 208px;
background-color: pink;
}
#phone {
position: relative;
width: 170px;
height: 381px;
background-color: #2858b9;
opacity: 1;
cursor: crosshair;
background-size: 100%;
z-index: 1000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<body>
<!-- Section: intro -->
<section id="top-bg">
<div class="landingtext">
<h1>Make paper come to life</h1>

<h3>with Interactive print by ori</h3>

</div>
</section>
<!-- /Section: intro -->
<!-- Section: demo -->
<section id="examples">
<div id="outer">
<div id="phone"></div>
<div id="thing1">Thing 1</div>
<div id="thing2">Thing 2</div>
<div id="thing3">Thing 3</div>
<div id="thing4">Thing 4</div>
<div id="thing5">Thing 5</div>
<div id="thing6">Thing 6</div>
<div id="thing7">Thing 7</div>
<div id="thing8">Thing 8</div>
</div>
</section>
<!-- /Section: demo -->
</body>

最佳答案

http://jsfiddle.net/gv8qv43b/4/ 做了一些改动:

  • pointer-events: none;#phone 上,这样当您点击时,它会通过 #phone 到达它后面的元素。
  • $("#outer > div").click() 所以只有点击 #thing 才有效果。 (#phone 包含在选择器中,但如上所述,它不能接收点击。)
  • var elem = this;。让浏览器完成繁重的工作并找出您点击的内容。不需要 elementFromPoint()

关于jquery "cannot read propery ' id' of null"when tracking event.pageX+Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com