当前位置: 移动技术网 > IT编程>开发语言>JavaScript > ReactJs设置css样式的方法

ReactJs设置css样式的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

前段时间看了react native,但是感觉在安卓反面的开发并不成熟.有较多功能有待完善,而且自己在实际运用的过程中在一些模块上遇到了不晓得阻力,又苦于网上没有找到那么多资源.于是打算先放一段时间,还是回过头来看reactjs吧.

react颠覆了html的传统思维,代码基本都写在<script  type="text/babel"></script>标签里面.我开发的时候采用的是idea,当然也可以使用atom或者webstor.使用idea时,需要在settings里面的language & framework设置javascript language version为jsx harmony.否则,编辑器可能会对你的正确语法进行报错.

<script src="../js/react.js"></script>

<script src="../js/react-dom.js"></script>

<script src="../js/browser.min.js"></script>

<script src="../js/jquery-1.7.2.min.js"></script>

html文件header标签里面引用上面前三个文件,就可以进行react开发了,但是由于jquery的ajax请求比较方便,所以这里我引用了jquery,当然也可以自己封装一个类似于ajax的方法,或者使用原生http请求与后台交互.

今天主要说说设置react样式的问题:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>react js</title>
  <script src="../js/react.js"></script>
  <script src="../js/react-dom.js"></script>
  <script src="../js/browser.min.js"></script>
  <script src="../js/jquery-1.7.2.min.js"></script>
  <style rel="stylesheet">
    .hello{
      color: #ffffff;width: 200px;height: 30px;border: none;background-color: #00a0e9;line-height: 30px;text-align: center;font-size: 14px;
      font-family: "microsoft yahei ui";cursor: pointer;
    }
    .redback{
      background-color: #f00;overflow: hidden;
    }
  </style>
</head>
<body>
<div id="msg"></div>
</body>
<script type="text/babel">
  var colorstyle={
    color: '#ffffff',
      width: 200,height: 30,border: 'none',backgroundcolor: '#00a0e9',textalign: 'center',fontsize: 14,
  fontfamily: "microsoft yahei ui",cursor: 'pointer',float:'left',lineheight:'30px'
  };
  var http=react.createclass({
    getinitialstate: function () {
      return{
        videosrc:"../src/demo1.mp4"
      }
    },
    handpost:function () {
//      var httprequest=new xmlhttprequest();
//      httprequest.open("get","/json/city");
//      httprequest.send();
//      httprequest.onreadystatechange =function () {
//        if(httprequest.readystate==4 && httprequest.status==200){
//          console.log(json.parse(httprequest.responsetext));
//        }
//      }
      $.ajax({
        type:'get',
        url:'/json/city',
        datatype:'json',
        success: function (data) {
          console.log(data)
        }
      })
    },
    render:function () {
      return(
          <div classname="redback">
            <video src={this.state.videosrc} style={{float:'left',width:300,border:'5px solid #ffffff'}} controls="controls"></video>
            <div onclick={this.handpost} style={colorstyle}>点击请求城市资源</div>
          </div>
          )
    }
  });
  reactdom.render(
      <http/>,document.getelementbyid('msg')
  )
</script>
</html>

如上代码所示,我觉得设置样式有三种方法:

1.如蓝色字体部分所示,直接写行内样式

2.在js代码中定义一个变量,然后在元素标签内部调用变量,如红色字体所示.

3.设置标签的classname,如绿色字体部分

下面附上截图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网