首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

jquery中div.css()添加样式的有关问题

2012-05-21 
jquery中div.css()添加样式的问题%@ Page LanguageC# AutoEventWireuptrueCodeFileDefault.aspx.

jquery中div.css()添加样式的问题
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>无标题页</title>
  <script language="javascript" type="text/javascript" src="JQ/jquery-1.7.2.js"></script>
  <style type="text/css">
  .parentDiv{
  width:1400px;
  height:550px;
  background-color:yellow;
  margin:0;
  padding:0;
  }
  </style>
  <script type="text/javascript" language="javascript">
  /* var createDiv= function(){ 
  var windowobj = $(window);
  var browserWidth = windowobj.width(); //浏览器的宽
  var browserHieght = windowobj.height(); //浏览器的高
  var scrollLeft = windowobj.scrollLeft(); //滚动条的横位置
  var scrollTop = windowobj.scrollTop(); //滚动条的竖位置
  var selfWidth = this.outerWidth(true); //这个元素的宽包括magin,padding
  var selfHeight = this.outerHeight(true); //这个元素的高包括magin,padding
  */

  var parentDiv=$('<div></div>'); //创建一个父DIV
  parentDiv.attr('id','parent'); //给父DIV设置ID
  parentDiv.addClass('parentDiv'); //添加CSS样式
  var childDiv=$('<div></div>'); //创建一个子DIV
  childDiv.attr('id','child'); //给子DIV设置ID

childDiv.removeClass();
 childDiv.css({'margin':0,
  'padding':0,
  ' width':100px,
  'height':40px,
  'background-color':red,
  'position': absolute,
  'top':200px,
  'left':200px}); 
   
  childDiv.appendTo(parentDiv);  
  parentDiv.appendTo('body'); //将父DIV添加到BODY中

  
}
  
  </script>
   
</head>
<body onload="createDiv();">
   
</body>
</html>
请问这段代码为什么什么都显示不出来。如果没有childDiv.css(省略)这段,而在<style>中设置.childDiv样式可以显示。为什么?

[解决办法]
1楼的根本就不行,匿名函数定义都被注销了,页面加载的时候怎么调用啊。。还有下面那些属性要加引号

HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">   <title>无标题页</title>   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>   <style type="text/css">   .parentDiv{   width:1400px;   height:550px;   background-color:yellow;   margin:0;   padding:0;   }   </style>   <script type="text/javascript" language="javascript">   var createDiv= function(){       /*  var windowobj = $(window);       var browserWidth = windowobj.width(); //浏览器的宽       var browserHieght = windowobj.height(); //浏览器的高       var scrollLeft = windowobj.scrollLeft(); //滚动条的横位置       var scrollTop = windowobj.scrollTop(); //滚动条的竖位置       var selfWidth = this.outerWidth(true); //这个元素的宽包括magin,padding       var selfHeight = this.outerHeight(true); //这个元素的高包括magin,padding       */   var parentDiv=$('<div></div>'); //创建一个父DIV   parentDiv.attr('id','parent'); //给父DIV设置ID   parentDiv.addClass('parentDiv'); //添加CSS样式   var childDiv=$('<div></div>'); //创建一个子DIV   childDiv.attr('id','child'); //给子DIV设置IDchildDiv.removeClass();  childDiv.css({'margin':'0',   'padding':'0',   ' width':'100px',   'height':'40px',   'background-color':'red',   'position': 'absolute',   'top':'200px',   'left':'200px'});        childDiv.appendTo(parentDiv);     parentDiv.appendTo('body'); //将父DIV添加到BODY中    }      </script>     </head><body onload="createDiv();">     </body></html> 

热点排行