Javascript 为什么可以';我的内容不会像这样在点击时出现吗?
我已经编写了一个Javascript 为什么可以';我的内容不会像这样在点击时出现吗?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我已经编写了一个javascript来显示隐藏的内容,但当我点击它时它就不会显示,有人能帮我吗 这是我的剧本: <script src="style/jquery/jquery.js"></script> <script> $(document).ready( function(){ $("#yes").click( function(){
javascript
来显示隐藏的内容,但当我点击它时它就不会显示,有人能帮我吗
这是我的剧本:
<script src="style/jquery/jquery.js"></script>
<script>
$(document).ready(
function(){
$("#yes").click(
function(){
$("div#did").show();
}
)
}
function(){
$("#no").click(
function(){
$("div#did_not").show();
}
)
}
)
</script>
和我的HTML:
<button id="yes">Yes</button>
<button id="no">No</button>
<div id="did">
<p>
Yes
<p/>
</div>
<div id="did_not">
<p>
No
</p>
</div>
是
不
对
不
帮帮我
show
是一个函数而不是属性。因此,您必须添加()
,才能使其正常工作
$("div#did_not").show();
因为你们将事物定义为函数,而不是调用它
$(document).ready(
function(){
$("#yes").click(function(){
$("div#did").show();
})
}(), //here you have to call the function to make it work by adding parentheses
function(){
$("#no").click(function(){
$("div#did_not").show();
})
}()
)
show
是一个函数而不是属性。因此,您必须添加()
,才能使其正常工作
$("div#did_not").show();
因为你们将事物定义为函数,而不是调用它
$(document).ready(
function(){
$("#yes").click(function(){
$("div#did").show();
})
}(), //here you have to call the function to make it work by adding parentheses
function(){
$("#no").click(function(){
$("div#did_not").show();
})
}()
)
我认为您的代码有语法错误,请尝试以下操作:
$(document).ready(function(){
$("#yes").click(function(){
$("div#did").show();
})
$("#no").click(function(){
$("div#did_not").show();
})
});
我认为您的代码有语法错误,请尝试以下操作:
$(document).ready(function(){
$("#yes").click(function(){
$("div#did").show();
})
$("#no").click(function(){
$("div#did_not").show();
})
});
虽然它不起作用的原因已经得到了回答(语法错误),但我建议将代码简化为:
$('button').on('click', '#yes, #no', function(){
$('#did' + (this.id === 'yes' ? '' : '_not')).show();
});
假设只有一个响应(鉴于答案“是”/“否”的布尔性质)应可见:
$('button').on('click', function(){
var response = $.trim($(this).text());
$('div.response').hide().filter(function(){
return $.trim($(this).text()) === response;
}).show();
});
参考资料:
$('button').on('click', '#yes, #no', function(){
$('#did' + (this.id === 'yes' ? '' : '_not')).show();
});
假设只有一个响应(鉴于答案“是”/“否”的布尔性质)应可见:
$('button').on('click', function(){
var response = $.trim($(this).text());
$('div.response').hide().filter(function(){
return $.trim($(this).text()) === response;
}).show();
});
参考资料:
我忘记了在那之前我已经发现了真正的问题:
我忘记了,
。/
在此之前,style/jquery/jquery.js
它是show(),缺少括号。应为$(“div#did”).show();将show改为show()。ready()
接受1个函数作为参数,您已经将两个函数作为单个参数依次放置,这是没有意义的。从代码中间删除}函数(){
,但它仍然没有…Shai…是的,我承认…它是show(),缺少括号。应该是$(“div#did”).show();将show改为show().ready()
接受1个函数作为参数,您将两个函数作为单个参数依次放置,这是没有意义的。删除}函数(){
从你的代码中间…它仍然没有…谢…是的,我承认…它可以是注释,你仍然没有,它可以是注释,你仍然没有,虽然我必须在三元组周围添加括号使其工作。它确实工作,尽管我必须在三元组周围添加括号使其工作。你是f吗熟悉JSIDdle?您可以检查JSIDdle演示是否正常工作,如果它在您的项目中不工作,可能您没有正确包含JQuery,请检查
,并尝试在浏览器中检查javascript错误。您熟悉JSIDdle吗?如果它在您的项目中不工作,您可以检查JSIDdle演示是否正常工作可能您没有正确地包含JQuery,请检查
,并尝试检查浏览器中的javascript错误。