当我单击链接 #S1 时,会出现一个带有列表的模式。然后,我单击列表中的一项以将各种信息发送到页面。它工作得很好。但是,当我再次单击 #S1 链接时,没有任何反应。我做错了什么?
这是html代码
<div class="slot" id="slot1">
<div class="image">
<a href="#"><span class="circled" id="S1"></span></a>
</div>
<div class="text">
</div>
</div>
这是 onclick 函数
$('#S1').click(function(){
openModal();
modalContent(1);
showList(1)
return false;
});
这是 openModal 函数
function openModal() {
el = document.getElementById("modal");
el.style.visibility = "visible";
}
这是 modalContent 函数
function modalContent(id) {
switch(id) {
case 1:
$("#modal").load("modal.php");
break;
}
}
这是 showList 函数
function showList(id) {
if (id=="") {
document.getElementById("list").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("list").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","list.php?slot="+id,true);
xmlhttp.send();
}
这是list.php代码
$slot = intval($_GET['slot']);
if($slot == 1){
$result = $db->query("SELECT * FROM item ORDER BY level DESC");
$result->setFetchMode(PDO::FETCH_OBJ);
echo '<table class="tableau">';
while($row = $result->fetch()){
echo '<tr onclick="itemInfo('.$row->id.')">';
echo '<td width="10%">'.$row->level.'</td>';
echo '<td width="90%">'.$row->name.'</td>';
echo '</tr>';
}
echo '</table>';
}
这是 itemInfo 函数
function itemInfo(id) {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById('slot1').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","itemChoice.php?item="+id,true);
xmlhttp.send();
document.getElementById('modal').innerHTML = '';
document.getElementById('modal').style.visibility = "hidden";
}
提前谢谢
最佳答案
我首先注意到在 click
函数中的 showList(1)
后面没有分号。您是否检查过浏览器中的错误控制台,看看是否由于 JS 错误而无法点击?
而且,我注意到您用 jquery
标记了它,但您几乎没有使用它。这是使用 jQuery 重构的 JavaScript 代码:
<script>
$(function() {
$('#S1').click(function(){
openModal();
modalContent(1);
showList(1);
return false;
});
});
function openModal() {
$("#modal").show();
}
function modalContent(id) {
switch(id) {
case 1:
$("#modal").load("modal.php");
break;
}
}
function itemInfo(id) {
$.get("itemChoice.php?item="+id, function(data) {
$("#slot1").html(data);
});
$("#modal").hide().html("");
}
function showList(id) {
$.get("list.php?slot="+id, function(data) {
$("#list").html( data );
});
}
</script>
哦,是的,除非有特定原因需要使用 visibility
样式,否则不要管它并使用 display: none;
和 display: block;
。 jQuery show()
和 hide()
函数使用 display
而不是 visibility
。
关于javascript - 做出选择后 div 不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25042411/