Jquery css("top") returns "undefined"
I am trying to set the property of a DIV element using the .css(...) method of Jquery, but it keeps returning "undefined" both before and after I set the value. Here is the code snippet:
var isense =
{
$dialog: $('#isense'),
$box: null, // gets set before call to ShowDialog
...
ShowDialog: function (data)
{
var height = this.$box.height() + 9;
var $offset = this.$box.offset();
var content = $('<div />').append($('#isensetemplate').tmpl(data)).html();
$('.isenseperson').remove();
this.$dialog.append(content);
this.$dialog.css("left", $offset.left);
this.$dialog.css("top", $offset.top + height);
this.$dialog.show();
},
...
}
I've stepped through in the IE9 debugger (my code has to run in IE), and this.$box is an object (jquery object) wrapping the element I am trying to show the DIV at. The $offset has the right numbers (they are positive and look to be the right location).
The problem is, after I call this.$dialog.css("left", $offset.left), the value of this.$dialog.css("left") is "undefined". It's "undefined" both before and after the call. The same for "top". Then the call to this.$dialog.show() doesn't seem to do anything, that is, I don't see the dialog (the style property for that dialog is initially set to include "display: none;").
Also, the content variable does hold some HTML, and it looks right, so I don't think that's the issue. Any ideas? I've seen the css properties return "undefined" before and I couldn't figure out why.
I am using Jquery 1.4.4, and the code is running inside a webbrowser control in .NET which is IE7. I ran it in IE9 and it too has the same issue.
UPDATE: I just trie开发者_StackOverflow社区d with the latest Jquery 1.5.2 and it still has the same problem.
Make sure that the variable $dialog is actually pointing to something. Maybe:
if ($dialog.length == 0) {
alert('$dialog variable is empty!')
}
And just for the hell of it, try it in a different browser.
The element has to exist before you can find it. Make sure that the object is created after the document has loaded:
$(document).ready(function(){
var isense = {
...
}
});
精彩评论