我的jQuery代码无法按预期工作。jQuery show()在Google Chrome中不起作用
下面是HTML:
<a href="">Some link</a>
<br/>
<a class="delete_file_initial" href="">Delete file</a>
<span class="delete_file_question hide">are you sure to delete?</span>
<a class="delete_file_question confirm hide" href="http://example.com/delete">yes</a>
<a class="delete_file_question cancel hide" href="">no</a>
<br/>
<a class="move_file_initial" href="">Move file</a>
<span class="move_file_question hide">are you sure to move?</span>
<a class="move_file_question confirm hide" href="http://example.com/move">yes</a>
<a class="move_file_question cancel hide" href="">no</a>
和JavaScript:
$(function() {
// delete file
$("a.delete_file_initial").click(function(event) {
event.preventDefault()
$(this).hide()
$(".delete_file_question").show()
});
$("a.delete_file_question").click(function(event) {
if ($(this).hasClass("cancel")) {
event.preventDefault()
$(".delete_file_question").hide()
$(".delete_file_initial").show()
}
});
// move file
$("a.move_file_initial").click(function(event) {
event.preventDefault()
$(this).hide()
$(".move_file_question").show()
});
$("a.move_file_question").click(function(event) {
if ($(this).hasClass("cancel")) {
event.preventDefault()
$(".move_file_question").hide()
$(".move_file_initial").show()
}
});
});
有两个相似的代码块:删除和移动。除了要加载的实际URL不同之外,它们应该执行相同的操作。 预期行为如下。用户点击链接“删除”/“移动”,然后显示确认链接,并确认或取消该操作。
问题是,当我点击链接“delete_file_initial”时,它隐藏,但链接“delete_file_question”不显示,留下一个空行。 “移动”块按预期工作。如果我评论“移动”块,“删除”块将按预期开始工作。更奇怪的是,这个错误出现在Google Chrome 22.0.1229.79中,但在Firefox 15.0.1中一切正常。
如何解决这个错误?
铬V.22没有给我任何问题:http://jsfiddle.net/mblase75/cg8Sh/ - 你有没有在控制台中的任何错误?一个想法是:'href'属性不应该是空的; [需要时使用'#'或'javascript:'作为“空href”](http://jsfiddle.net/mblase75/cg8Sh/3/)(总是用分号结束JavaScript行也是一个好习惯) – Blazemonger
感谢您的评论。不,我在控制台中没有任何错误(即使在向html添加有问题的'
'后,请参阅我的答案)。它可能是一个jQuery的错误(我使用1.4.4)? –