【问题标题】:necessity of class attribute in order to position div layer类属性的必要性以定位 div 层
【发布时间】:2012-12-28 19:23:03
【问题描述】:

我很难理解为什么(而不是如何)必须将类属性分配给 div 以更改其位置。例如,以下代码不会将 div 移动到 200,200:

this.m_textLayer = document.createElement( 'div' ); 
this.m_textLayer.setAttribute( 'id', 'textLayer' ); 
this.m_textLayer.setAttribute( 'position', 'absolute' ); 
this.m_textLayer.setAttribute( 'left', '0px' ); 
this.m_textLayer.setAttribute( 'top', '0px' ); 
this.m_textLayer.setAttribute( 'width', '300px' ); 
this.m_textLayer.setAttribute( 'height', '100px' ); 
this.m_baseLayer.appendChild( this.m_textLayer );
$( '#textLayer' ).append( $( 'p' ) ).text( 'test' );
// some time later
$( '#textLayer' ).css( { left:'200px', top:'200px' } ); 

但是让'class'属性指向具有相同属性的css中的类定义确实可以按预期工作:

this.m_textLayer = document.createElement( 'div' ); 
this.m_textLayer.setAttribute( 'id', 'textLayer' ); 
this.m_textLayer.setAttribute( 'class', 'Layer' ); 
this.m_baseLayer.appendChild( this.m_textLayer );
$( '#textLayer' ).append( $( 'p' ) ).text( 'test' );
// some time later
$( '#textLayer' ).css( { left:'200px', top:'200px' } ); 

现在,我可以推测原因,因为它可能与未设置类 attr 时没有定义 css 样式有关,但有人可以向我详细解释一下,或者指出我一些资源可以准确地解释这里实际发生的事情? (我绝对不是编程新手,但我是 css/js/jquery 新手,很难找到关于 js 解释的实际机制的信息)。

【问题讨论】:

    标签: javascript jquery css html css-position


    【解决方案1】:

    我很难理解为什么(而不是如何)必须将类属性分配给 div 以更改其位置。

    不是。您正在使用positionleft 之类的名称设置属性,但这些不是属性,它们是样式属性。所以改变:

    this.m_textLayer.setAttribute( 'position', 'absolute' ); 
    this.m_textLayer.setAttribute( 'left', '0px' ); 
    this.m_textLayer.setAttribute( 'top', '0px' ); 
    this.m_textLayer.setAttribute( 'width', '300px' ); 
    this.m_textLayer.setAttribute( 'height', '100px' ); 
    

    this.m_textLayer.style.position = 'absolute' ; 
    this.m_textLayer.style.left = '0px'; 
    this.m_textLayer.style.top = '0px'; 
    this.m_textLayer.style.width = '300px'; 
    this.m_textLayer.style.height = '100px'; 
    

    旁注:

    由于您使用的是 jQuery,因此您也可以这样做:

    this.m_textLayer = $( '<div>' )
                       .attr( 'id', 'textLayer' )
                       .css({
                           position: 'absolute',
                           left:     '0px',
                           top:      '0px',
                           width:    '300px',
                           height:   '100px'
                       })
                       .append( $( 'p' ) )
                       .text( 'test' )
                       .appendTo(this.m_baseLayer)[0];
    
    // some time later
    $( '#textLayer' ).css( { left:'200px', top:'200px' } ); 
    

    【讨论】:

    • 哦,天哪,我觉得这里有点愚蠢。我的感觉是css设置了属性,但事实并非如此。这当然可以解决问题! :o
    • @JochemVanDerSpek:很高兴有帮助!
    • 单个样式更改:$('#div').css('left', '10px'); 和多个样式更改; $('#div').css({'left' : '10px', 'width' : '300px'});
    • 推荐这本 Jquery 书 - books.google.com/books/about/…
    【解决方案2】:

    试试这个:

    this.m_textLayer.style.position = 'absolute'; 
    this.m_textLayer.style.left = '0px'; 
    this.m_textLayer.style.top = '0px'; 
    this.m_textLayer.style.width = '300px'; 
    this.m_textLayer.style.height = '100px';
    

    它不使用类,而是为元素设置内联样式。您正在做的是设置属性而不是样式。很多这些属性根本不存在,所以浏览器会忽略它们。

    您的代码在 DOM 方面的产品如下所示:

    <!-- Assuming m_textLayer is a DIV -->
    <div position="absolute" left="0px" top="0px" width="300px" height="300px" />
    

    设置样式的结果是:

    <!-- Assuming m_textLayer is a DIV -->
    <div style="position: absolute; left: 0px; top: 0px; width: 300px; height: 300px" />
    

    【讨论】:

      【解决方案3】:

      你似乎混合了 javascript 和 jQuery 来做同样的工作,不是 100% 确定为什么?

      但这不是导致问题的原因,您正在尝试设置属性而不是样式元素。

      你目前正在构建类似的东西:

      <span id="textLayer" position="absolute" left="0px" top="0px" width="300px" height="100px" />
      

      试试这个:

      var m_baseLayer = document.getElementById('baselayer');
      this.m_textLayer = document.createElement( 'div' ); 
      this.m_textLayer.setAttribute( 'id', 'textLayer' ); 
      this.m_textLayer.style.position = 'absolute' ; 
      this.m_textLayer.style.left = '0px'; 
      this.m_textLayer.style.top = '0px'; 
      this.m_textLayer.style.width = '300px'; 
      this.m_textLayer.style.height = '50px'; 
      this.m_textLayer.style.background = 'green'; 
      m_baseLayer.appendChild( this.m_textLayer );​
      

      见:http://jsfiddle.net/GPkMk/

      【讨论】:

        猜你喜欢
        • 2014-04-22
        • 2013-02-24
        • 1970-01-01
        • 1970-01-01
        • 2010-10-20
        • 2017-06-19
        • 2016-10-11
        • 2014-12-08
        • 1970-01-01
        相关资源
        最近更新 更多