最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - width() returning `null` on a DOM object - Stack Overflow

programmeradmin7浏览0评论

I have the following object

<div class='app_advert'><a href="/apps">GET THE APPS</a></div>

whicch I'll refer to as x I am trying to position automatically above a another element (y), I want to line up the middle of the objects, so my theory goes thus:

x_width = $(x).width();
y_width = $(y).width();
y_position = $(y).offset().left;

x_horizontal_position =  y_position+(y_width/2)-(x_width/2)

I am trying to fetch the width of x using:

jQuery(window).load(function(){
    object_width = jQuery('.apps_advert').width();
    console.log(object_width);
});

but it is returning null.

I am also applying the following CSS, in case this may affect things:

    div.app_advert{
    position: absolute;
    display: none;

    font-size: 17pt;
    top: 0;

}
div.app_advert a{
    color: white;
    text-decoration: none;
}

I have the following object

<div class='app_advert'><a href="/apps">GET THE APPS</a></div>

whicch I'll refer to as x I am trying to position automatically above a another element (y), I want to line up the middle of the objects, so my theory goes thus:

x_width = $(x).width();
y_width = $(y).width();
y_position = $(y).offset().left;

x_horizontal_position =  y_position+(y_width/2)-(x_width/2)

I am trying to fetch the width of x using:

jQuery(window).load(function(){
    object_width = jQuery('.apps_advert').width();
    console.log(object_width);
});

but it is returning null.

I am also applying the following CSS, in case this may affect things:

    div.app_advert{
    position: absolute;
    display: none;

    font-size: 17pt;
    top: 0;

}
div.app_advert a{
    color: white;
    text-decoration: none;
}
Share Improve this question asked Jun 20, 2011 at 12:25 Mild FuzzMild Fuzz 30.9k34 gold badges105 silver badges152 bronze badges 2
  • 1 Your checking apps_advert against app_advert. Also checking against a class won't return a single element. Need to set id=app_advert and check #app_advert. – BentFX Commented Jun 20, 2011 at 12:27
  • What does console.log(jQuery('.apps_advert').length) give you? – Mutation Person Commented Jun 20, 2011 at 12:29
Add a ment  | 

3 Answers 3

Reset to default 3

Your class is app_advert but you jQuery code tries to find apps_advert (notice the plural of app to apps)

so try with

object_width = jQuery('.app_advert').width();

If its display: none it has no width. (If null or 0 is the correct value I don't know.)

EDIT: As the others noticed you have a typo in the code "apps" vs "app" in which case null is expected. But still if the element is hidded using display: none, the width should be 0.

change this

object_width = jQuery('.apps_advert').width(); 

to this

object_width = jQuery('.app_advert').width(); 

your div has app_advert class but in your code you have writen apps_advert, and this class doesn't exist. This is the reason why you are getting null

发布评论

评论列表(0)

  1. 暂无评论