Javascript nextSibling is not working [duplicate]

橙三吉。 提交于 2019-12-25 19:05:51

问题


I have followed html:

<tr>
    <td class="link" onClick="detil_iklan(this)">AD2188914</td>
    <td>Seluruh Zona 468x60 - Global Gambar</td>
    <td>41.100.000</td>
    <td>4.440.100</td>
    <td>0.005</td>
    <td>Rp. 1.250.000</td>
</tr>
<tr class="border_bottom detil_iklan" style="display:none">
    <td colspan="6">AD2188914</td>
</tr>

I have followed javascript:

function detil_iklan(e) {
        var parent = e.parentNode;
        var next = parent.nextSibling;
        if (next.style.display == 'none') {
            next.style.display = '';
        } else {
            next.style.display = 'none';
        }
        alert("fas");
    }

what wrong with my code ? When call detil_iklan(e), alert is not called. Can anyone help me ?

thanks,


回答1:


Added a function to find next sibling

function detil_iklan(e) {
    var parent = e.parentNode;
    var next = get_nextsibling(parent);
    if (next.style.display == 'none') {
            next.style.display = '';
        } else {
            next.style.display = 'none';
        }
}

//function to find next sibling  
function get_nextsibling(n) {
    x = n.nextSibling;
    while (x.nodeType != 1) {
        x = x.nextSibling;
    }
    return x;
}

DemoFiddle



来源:https://stackoverflow.com/questions/22588071/javascript-nextsibling-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!