Javascript 无法获取div宽度/高度
我发现了一个很好的示例,演示了如何在单击附近显示弹出窗口: 我利用它来点击按钮,但我似乎无法获得元素的实际宽度或高度,我也不完全确定原因:Javascript 无法获取div宽度/高度,javascript,jquery,css,jqote,Javascript,Jquery,Css,Jqote,我发现了一个很好的示例,演示了如何在单击附近显示弹出窗口: 我利用它来点击按钮,但我似乎无法获得元素的实际宽度或高度,我也不完全确定原因: function BindObject(o) { o.click(function(e) { var editor = $(this).find(".object_editor"); console.log(editor); console.log(editor.height());
function BindObject(o)
{
o.click(function(e)
{
var editor = $(this).find(".object_editor");
console.log(editor);
console.log(editor.height());
});
}
以下是打印到控制台的内容:
(仅供参考,BindObject是在DocumentReady上调用的,我也在使用jqote,不确定它的相关性有多大)
它显然找到了元素(由编辑器表示),但我无法确定它的宽度或高度。我知道display设置为none,但那是因为我不想显示它,直到有人单击按钮显示弹出窗口。有人知道为什么它总是空的吗
以下是css:
.object_editor{
position:absolute;
z-index:10;
width:172px;
height:102px;
text-align:center;
color:#FFFFFF;
font: 14px Verdana, Arial, Helvetica, sans-serif;
background-color:#000000;
display:none;
}
编辑:以及相关的jqote,以备需要:
<!-- Object Template -->
<script type="text/x-jqote-template" id="ObjectTemplate">
<![CDATA[
<span class="object <%= (this.hidden ? "hiddenType" : "") %>">
<input class="objectType" type="hidden" name="<%= this.key[0] %>" value="<%= this.type %>">
<input class="objectEntry" type="hidden" name="<%= this.key[1] %>" value="<%= this.entry %>">
<a data-emptytext="<%= object_types[this.type] %>" data-name="<%= this.key[1] %>" target="_blank" href="<%= (this.entry? whUrl(this.type, this.entry) : "#") %>" class="objectName"><%= this.name %><%= (this.entry?" ("+this.entry+")":"") %></a> <i class="icon-edit editObject"></i>
<% if(this.remove) { %>
<a href=".object" class="deleteParent"><i class="icon-trash"></i></a>
<% } %>
</span>
<!-- Insert our Editor -->
<%=$("#ObjectEditorTemplate").jqote({type: this.type, entry:this.entry}) %>
]]>
</script>
<!-- Object Editor Template -->
<script type="text/x-jqote-template" id="ObjectEditorTemplate">
<![CDATA[
<div class="object_editor">My Editor: <%= this.entry %></div>
]]>
</script>
]]>
我的编辑:
]]>
试试这个$('divID').css('width')代码>
与高度类似,原因是“显示”设置为“无”。类似的问题请参见。我尝试使用可见性:隐藏;相反,当我执行editor.width或editor.heights时,它仍然会给我一个null值。一旦我在中移动它,它就会正确地找到它,并且我也需要上面的内容。谢谢添加更多信息后,页面可能会有多个名为object_editor的类,但不管这是为什么我使用.find,所以我认为这不重要
<!-- Object Template -->
<script type="text/x-jqote-template" id="ObjectTemplate">
<![CDATA[
<span class="object <%= (this.hidden ? "hiddenType" : "") %>">
<input class="objectType" type="hidden" name="<%= this.key[0] %>" value="<%= this.type %>">
<input class="objectEntry" type="hidden" name="<%= this.key[1] %>" value="<%= this.entry %>">
<a data-emptytext="<%= object_types[this.type] %>" data-name="<%= this.key[1] %>" target="_blank" href="<%= (this.entry? whUrl(this.type, this.entry) : "#") %>" class="objectName"><%= this.name %><%= (this.entry?" ("+this.entry+")":"") %></a> <i class="icon-edit editObject"></i>
<% if(this.remove) { %>
<a href=".object" class="deleteParent"><i class="icon-trash"></i></a>
<% } %>
</span>
<!-- Insert our Editor -->
<%=$("#ObjectEditorTemplate").jqote({type: this.type, entry:this.entry}) %>
]]>
</script>
<!-- Object Editor Template -->
<script type="text/x-jqote-template" id="ObjectEditorTemplate">
<![CDATA[
<div class="object_editor">My Editor: <%= this.entry %></div>
]]>
</script>