javascript - 做出选择后 div 不可点击

标签 javascript jquery html

当我单击链接 #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/

相关文章:

javascript - 类型错误 : Cannot read property 'create' of undefined (Material UI/enzyme)

javascript - 带有单选按钮的 jQuery post 函数

javascript - Bootstrap - 键盘无法在动态生成的文本框软搜索菜单上工作

html - 两个 Div 没有并排对齐

css - 问题放置三分;第三个 div 以某种方式影响整个页面

Html 类型值无效

javascript - 为什么我不能删除段落?

Javascript:通过内部数组的键将一个对象插入另一个数组内的数组中

javascript - 当我将 div 附加到准备好的文档上时,我的 div 文本没有更改。

javascript - 不需要的图标出现在 IE8 中的图像上