Created
June 21, 2013 02:04
-
-
Save mawatari/5828359 to your computer and use it in GitHub Desktop.
jQueryでインデックスを指定して要素を追加する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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