Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>.addClss()方法</h2>
<div class="left">
  <div class="aaron">
    <p>newClass</p>
  </div>
  <div class="aaron">
    <p>newClass</p>
  </div>
</div>
<div class="right">
  <div class="aa bb imooc">
    <article>
      <p>imoocClass</p>
    </article>
  </div>
  <div class="bb cc imooc ">
    <article>
      <p>imoocClass</p>
    </article>
  </div>
</div>
              
            
!

CSS

              
                .left,
.right {
  width: 300px;
  height: 120px;
}

.left div,
.right div {
  width: 100px;
  height: 90px;
  padding: 5px;
  margin: 5px;
  float: left;
  border: 1px solid #ccc;
}

.newClass {
  background: #bbffaa;
}

.imoocClass {
  background: red;
}

              
            
!

JS

              
                //class=left下div元素增加一个新的样式,增加背景颜色
$(".left div").addClass("newClass");
//通过className(fucntion)方法
//这个函数返回一个或更多用空格隔开的要增加的样式名。
//接收index 参数表示元素在匹配集合中的索引位置和html 参数表示元素上原来的 HTML 内容

//找到所有的div,然后通过addClass设置颜色,根据返回的className的判断,
$("div").addClass(function(index, className) {
  console.log(index);
  //找到类名中包含了imooc的元素
  if (-1 !== className.indexOf("imooc")) {
    //this指向匹配元素集合中的当前元素
    $(this).addClass("imoocClass");
  }
});

              
            
!
999px

Console