简体   繁体   English

jQuery .parents([selector])不适用于IE9和Firefox

[英]jQuery .parents([selector]) doesn't work with IE9 and Firefox

<html>
<body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="application/javascript">

        $(document).ready(function() {
            $("#evaluation_complete").parents("table")[0].remove(); //doesn't work

            //this works
            //var EvalComplete = document.getElementById("evaluation_complete");
            //EvalComplete.parentNode.parentNode.parentNode.parentNode.parentNode.removeChild(
                //EvalComplete.parentNode.parentNode.parentNode.parentNode); 
        });

    </script>
    <p>Testing little code</p>
    <table>
        <tbody>
            <tr>
                <td class="button-left">&nbsp;</td>
                <td class="button-middle" nowrap="true"><div
                        style="margin: 0px 0px 1px;">
                        <a class="button-text" name="evaluation_complete"
                            id="evaluation_complete" href="#">Evaluation Complete</a>
                    </div></td>
                <td class="button-right">&nbsp;</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

I have no control of how the table is set up. 我无法控制表格的设置。 However all I know is the id of the link. 但是,我所知道的只是链接的ID。 My goal is to traverse to the <table> element and remove it from DOM. 我的目标是遍历<table>元素并将其从DOM中删除。 I've also tried it with closest . 我也尝试过closest它。 The error I get in Firefox and IE9 is that remove is not a function. 我在Firefox和IE9中收到的错误是remove不是一个功能。 The commented out block works but is not very dynamic. 注释掉的块有效,但不是很动态。 However, in Chrome, it works flawlessly. 但是,在Chrome中,它可以完美运行。 Thank you for any help. 感谢您的任何帮助。

It does'nt work as you are trying to use .remove() on a native JS element, not a jQuery element: 当您尝试在本机JS元素而不是jQuery元素上使用.remove()时,它不起作用:

It's not : 不是 :

$("#evaluation_complete").parents("table")[0].remove();

but

$("#evaluation_complete").parents("table").eq(0).remove();

or 要么

$("#evaluation_complete").parents("table").first().remove();

etc. 等等

using [0] or get(0) gets the native JS element from the array-like jQuery object, which does'nt have a .remove() method. 使用[0]get(0)从类似数组的jQuery对象中获取本机JS元素,该对象没有.remove()方法。

As a sidenote, using closest() would be more efficient, and will work with the above examples. 附带说明一下,使用closest()会更有效,并且可以与上述示例配合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM