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

              
                <h1>jQueryとjavascriptの比較 - 要素編</h1>

<div class="block">
  <h2>要素の取得</h2>
 
    <h3>親要素の取得</h3>
    <p>jQuery</p>
    <div class="word_block">
      <p>子要素からみて親要素の背景が赤くなるクラスの追加</p>

      <div class="jq_wrap">
        <p id="jq_child">子要素</p>
      </div>
    </div>

    <p>javascript</p>
    <div class="word_block">
      <div class="js_wrap">
        <p id="js_child">ボタン</p>
      </div>
    </div>

</div>

<div class="block">
 
    <h3>子要素の取得</h3>
    <p>jQuery</p>
    <div class="word_block">
      <p>ボーダーの親要をクリックすると子要素であれテキストの背景が赤くなる</p>

      <div id="jq_parent" class="jq_wrap">
        <p>子要素のテキスト</p>
      </div>
    </div>

    <p>javascript</p>
    <div class="word_block">
      <div id="js_parent" class="js_wrap">
        <p>子要素のテキスト</p>
      </div>
    </div>

</div>


<div class="block">
 
    <h3>兄弟要素(直前と直後)の取得</h3>
    <p>jQuery</p>
    <div class="word_block">
      <p>リストの2番目の要素の前後の要素が背景が赤くなるクラスを追加</p>

      <div class="jq_wrap">
        <ul>
          <li>1番目の要素</li>
          <li id="jq_list_second">2番目の要素</li>
          <li>3番目の要素</li>
        </ul>
      </div>
    </div>

    <p>javascript</p>
    <div class="word_block">
      <div class="js_wrap">
        <ul>
          <li>1番目の要素</li>
          <li id="js_list_second">2番目の要素</li>
          <li>3番目の要素</li>
        </ul>
      </div>
    </div>

</div>


<div class="block">
 
    <h3>最初と最後の子要素の取得</h3>
    <p>jQuery</p>
    <div class="word_block">
      <p>最初と最後の要素のみ背景を赤くする</p>

      <div id="jq_fl_wrap" class="jq_wrap">
        <p>これは最初の要素</p>
        <p>テキストテキスト</p>
        <p>これは最後の要素</p>
      </div>
    </div>

    <p>javascript</p>
    <div class="word_block">
      <div id="js_fl_wrap" class="js_wrap">
         <p>これは最初の要素</p>
        <p>テキストテキスト</p>
        <p>これは最後の要素</p>
      </div>
    </div>

</div>
<div class="block">
 
    <h3>子孫要素を検索して取得</h3>
    <p>jQuery</p>
    <div class="word_block">
      <p>親要素から特定のpタグとli要素を検索して背景を赤くなるクラスを追加する</p>

      <div id="jq_find_wrap" class="jq_wrap">
        <p>このテキストの背景は赤くしない</p>
        <ul>
          <li>リスト要素</li>
          <li>リスト要素</li>
        </ul>
        <p id="jq_txt">このテキストの背景を赤くする</p>
      </div>
    </div>

    <p>javascript</p>
    <div class="word_block">
      <div id="js_find_wrap" class="js_wrap">
        <p>このテキストの背景は赤くしない</p>
        <ul>
          <li>リスト要素</li>
          <li>リスト要素</li>
        </ul>
        <p id="js_txt">このテキストの背景を赤くする</p>
      </div>
    </div>

</div>
              
            
!

CSS

              
                h1{
  font-size:18px;
  text-align:center;
  padding-bottom:1px solid #333;
  margin-bottom:30px;
}

p{
  font-size:14px;
}

h2{
  font-size:18px;
  text-align:center;
  padding-bottom:1px solid #333;
  margin-bottom:30px;
}

h3{
  font-size:16px;
  border-left:3px solid #ff0000;
  padding-left:10px;
}

.block{
  padding:20px;
  border-bottom:1px solid #ccc;
}

.jq_wrap,
.js_wrap{
  border:2px solid #ccc;
  padding:10px;
}

.addclass_red{
  background-color:#ff0000;
}
              
            
!

JS

              
                // 親要素取得
var parent = $("#jq_child").parent();
parent.addClass("addclass_red");

//子要素取得
$("#jq_parent")
  .children()
  .addClass("addclass_red");

//隣接する用を取得
var next_dom = $("#jq_list_second").next();
next_dom.addClass("addclass_red");

var prev_dom = $("#jq_list_second").prev();
prev_dom.addClass("addclass_red");

//findで検索してクラスを追加する
$("#jq_find_wrap")
  .find("p#jq_txt,li")
  .addClass("addclass_red");

//最初と最後の要素を検索して背景を赤くする
$("#jq_fl_wrap p:first-child").addClass("addclass_red");
$("#jq_fl_wrap p:last-child").addClass("addclass_red");

// javascript
var jq_child = document.getElementById("js_child");
var js_parent = document.getElementById("js_parent");
var js_list_second = document.getElementById("js_list_second");
var js_find_wrap = document.getElementById("js_find_wrap");
var js_fl_wrap = document.getElementById("js_fl_wrap");

//親要素を取得
var parent = jq_child.parentNode;
parent.classList.add("addclass_red");

//子要素を取得
var child = js_parent.children;
child[0].classList.add("addclass_red");

//隣接する要素を取得

var next_dom = js_list_second.nextElementSibling;
next_dom.classList.add("addclass_red");

var prev_dom = js_list_second.previousElementSibling;
prev_dom.classList.add("addclass_red");

//最初と最後の要素を検索して赤くする
var first_child = js_fl_wrap.firstElementChild;
first_child.classList.add("addclass_red");

var last_child = js_fl_wrap.lastElementChild;
last_child.classList.add("addclass_red");

//findで検索して要素を赤くする
var find_dom = js_find_wrap.querySelectorAll("p#js_txt,li");
for (var i = 0; i <= find_dom.length; i++) {
  find_dom[i].classList.add("addclass_red");
}

              
            
!
999px

Console