'position().top'为null或不是对象

'position().top' is null or not an object?

本文关键字:对象 null top position      更新时间:2023-09-26

我有一个代码,应该突出显示一个选择,并将弹出窗口与选择的顶部对齐。代码如下

$('#pnlAdd .modalDialog').css($("tr [style*=Blue]").position().top + "px" ,"top");

Firefox似乎可以处理代码,但我得到了以下运行时错误

'position().top' is null or not an object. 

找到了此链接,但我的编辑不成功。还有别的办法吗?

您的.css()参数是向后的;应该是.css(<name of property>, <value>)

$('#pnlAdd .modalDialog').css("top", $("tr [style*=Blue]").position().top + "px");

此外,如果$("tr [style*=Blue]")不返回任何内容,则.position()将返回null,从而导致错误。试着先检查一下它的长度。

var elements = $("tr [style*=Blue]");
if (elements.length) {
    $('#pnlAdd .modalDialog').css("top", elements.position().top + "px");
}

编辑注释Firefox在颜色上使用首字母大写(蓝色),但IE使用全小写(蓝色)。