JQuery: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 10: | Line 10: | ||
=== Filters === | === Filters === | ||
* <code>$('tr:eq(0)')</code> finds the first row in a table | |||
* <code>$('tr:gt(0)')</code> finds all rows after the first | |||
* <code>:even</code> finds all even-numbered elements in a selection | |||
* <code>:odd</code> odd-numbered elements | |||
* <code>:last</code> last element | |||
* <code>:empty</code> elements that have no children | |||
* <code>:focus</code> the focused element | |||
* <code>:not(selection)</code> elements that don't match selection | |||
* <code>:contains(text)</code> element that contain certain text | |||
* input field selectors: <code>:hidden</code>, <code>:text</code>, <code>:checkbox</code>, <code>:password</code> | |||
=== Combinations === | |||
* <code>$('#tasks tr:first')</code> The first row of a table with id 'tasks' | |||
* <code>$('#tasks').find('tr:first')</code> Same thing | |||
* <code>$('tr:first', '#tasks')</code> Same thing; second argument specifies DOM sub-tree to search | |||
== Example == | == Example == |
Revision as of 20:19, 21 April 2015
Selectors
- type:
$('table')
- attribute:
$('[date="2015-04-21"]')
- class:
$('.even')
- id:
$('#main')
Filters
$('tr:eq(0)')
finds the first row in a table$('tr:gt(0)')
finds all rows after the first:even
finds all even-numbered elements in a selection:odd
odd-numbered elements:last
last element:empty
elements that have no children:focus
the focused element:not(selection)
elements that don't match selection:contains(text)
element that contain certain text- input field selectors:
:hidden
,:text
,:checkbox
,:password
Combinations
$('#tasks tr:first')
The first row of a table with id 'tasks'$('#tasks').find('tr:first')
Same thing$('tr:first', '#tasks')
Same thing; second argument specifies DOM sub-tree to search
Example
HTML
<html><head><title>Button Magic</title>
<link rel='stylesheet' type='text/css' href='stylesheet.css'/>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript' src='script.js'></script>
</head><body>
<div><br/><strong>Click Me!</strong></div>
</body>
</html>
stylesheet.css
div {
height: 60px;
width: 100px;
border-radius: 5px;
background-color: #69D2E7;
text-align: center;
color: #FFFFFF;
font-family: Verdana, Arial, Sans-Serif;
opacity: 0.5;
}
script.js
$(document).ready(function(){
$('div').mouseenter(function(){
$('div').fadeTo('fast', 1.0);
});
$('div').mouseleave(function(){
$('div').fadeTo('fast', 0.5);
});
});
To Do List Example
HTML
<h2>To Do</h2>
<form name="checkListForm">
<input type="text" name="checkListItem"/>
</form>
<div id="button">Add!</div>
<br/>
<div id="list"></div>
script.js
$(document).ready(function(){
$('#button').click(function(){
var toAdd = $('input[name=checkListItem]').val();
$('#list').append('<div class="item">' + toAdd + '</div>');
});
$(document).on('click', '.item', function(){
$(this).remove()
});
});