我发现很难理解为什么(而不是如何)必须将类属性分配给 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 解释的实际机制的信息)。