gpt4 book ai didi

javascript - JQuery 不适用于本地机器上的点击事件,

转载 作者:行者123 更新时间:2023-11-29 18:07:34 25 4
gpt4 key购买 nike

JQuery 不适用于本地机器上的点击事件,但可以在 JSfiddle 上正常工作,请参阅此链接 JSFiddle .在下面的代码中,我试图隐藏和显示 div 的其余部分。以下代码在 jsfiddle 上运行良好,但在本地计算机上单击没有任何响应。请告诉我哪里错了..

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
$("#grid_content").on("click", ".toggle", function(evt) {
evt.preventDefault(); // Prevent window following #hash / jump
var more = $(this).text() === "Show More";
$(this).text(more ? "Show Less" : "Show More").prev(".restPart").slideToggle();
});
</script>

<style type="text/css">
.bucket {
width: 290px;
float: left;
margin: 0 0 48px 20px;
border: 1px solid #262626;
background: lightgray;

}
.restPart{
overflow:auto;
display:none; /* hide initially */
}

</style>
</head>

</body>
<section id="grid_content">

<div class="bucket">
<p>Visible part....</p>
<div class="restPart">
<p>Content...</p>
</div>
<a href="#" class="toggle">Show More</a>
</div>

<div class="bucket">
<p>Visible part....</p>
<div class="restPart">
<p>Content...</p>
</div>
<a href="#" class="toggle">Show More</a>
</div>

</section>
</body>
</html>

最佳答案

把你的代码放在里面:

$( document ).ready(function() {
//code here
});

A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you.

它在 jsfiddle 中工作,因为选择了 onload,所以它会自动将您的代码包装在 $(whindow).load(function(){}) 中:

Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ).load(function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready.

Reference

关于javascript - JQuery 不适用于本地机器上的点击事件,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30156618/

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