Skip to content

Instantly share code, notes, and snippets.

@mawatari
Created June 21, 2013 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mawatari/5828359 to your computer and use it in GitHub Desktop.
Save mawatari/5828359 to your computer and use it in GitHub Desktop.
jQueryでインデックスを指定して要素を追加する
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>デモ</title>
</head>
<body>
<ul id="list">
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
<script type="text/javascript" src="assets/lib/jquery-2.0.2.min.js"></script>
<script type="text/javascript">
var newListItem = $('<li>').text('add text');
// 以下の2通りで追加可
newListItem.insertBefore($('#list').children()[1]);
$($('#list').children()[1]).before(newListItem);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment