[英].show() doesn't show!
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready({
$('#sh').click(function(){
$('#container').show();
});
});
</script>
<style type="text/css">
#container {
width:350px;
height:350px;
border:1px solid #000000;
display:none;
}
</style>
</head>
<body>
<div id="container">
<p>paragraph 1</p>
</div>
<div>
<a href="#" id="sh">show</a>
</div>
</body>
</html>
clicking on link doesn't trigger .show() at all! 点击链接根本不会触发.show()! What am i doing wrong??
我究竟做错了什么?? Update: i tried $(document).ready({... it doesn't help!
更新:我试过$(document).ready({...这没有帮助!
A few things to keep in mind, load the scripts only after the document has finished loading: 请注意,只有在文档加载完成后才加载脚本:
$(function(){
/* Scripts here will run only
after the document has loaded */
});
Also, be sure to prevent the default behavior of links when using them to trigger actions: 另外,在使用链接触发操作时,请确保防止链接的默认行为 :
$(function(){
$("#sh").click(function(e){
e.preventDefault();
$("#container").show();
});
});
With these changes, the code works as expected: http://jsfiddle.net/apsje/ 经过这些更改,代码可以按预期工作: http : //jsfiddle.net/apsje/
add your jquery code between this 在这之间添加您的jQuery代码
$(document).ready(function() {
...
});
Use this: 用这个:
Because if you call your script before the real HTML, the elements you're referring to doesn't exist yet. 因为如果您在真正的HTML之前调用脚本,那么您所引用的元素尚不存在。
$(document).ready( function() {
$('#sh').click(function(){
$('#container').show();
})
By using the ready event on $(document), you make sure the DOM is completely loaded and ready to be interacted with. 通过在$(document)上使用ready事件,可以确保DOM已完全加载并可以进行交互。
The browser has not yet rendered #sh when you are binding the click. 绑定点击时,浏览器尚未呈现#sh 。
Try the following: 请尝试以下操作:
$(document).ready(function() {
$('#sh').click(function(){
$('#container').show();
});
});
It binds the click after the document has loaded. 加载文档后,它将绑定单击。
Your code is running before the elements are present in the DOM. 您的代码在DOM中存在元素之前就已运行。 Your script block should look like:
您的脚本块应如下所示:
<script type="text/javascript">
$( function()
{
$( '#sh' ).click( function()
{
$( '#container' ).show();
} );
} );
</script>
或将脚本放在</body>
之前
$(document).ready(function(){
$('#sh').click(function(){
$('#container').show();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.