JQuery: Difference between revisions

From Wiki
Jump to navigation Jump to search
No edit summary
Line 7: Line 7:


==== HTML ====
==== HTML ====
<pre>
<source lang="html4strict">
<html><head><title>Button Magic</title>
<html><head><title>Button Magic</title>
     <link rel='stylesheet' type='text/css' href='stylesheet.css'/>
     <link rel='stylesheet' type='text/css' href='stylesheet.css'/>
Line 15: Line 15:
     </body>
     </body>
</html>
</html>
</pre>
</source>


==== stylesheet.css ====
==== stylesheet.css ====
<pre>
<source lang="css">
div {
div {
     height: 60px;
     height: 60px;
Line 29: Line 29:
     opacity: 0.5;
     opacity: 0.5;
}
}
</pre>
</source>


==== script.js ====
==== script.js ====
<pre>
<source lang="javascript">
$(document).ready(function(){
$(document).ready(function(){
     $('div').mouseenter(function(){
     $('div').mouseenter(function(){
Line 41: Line 41:
     });
     });
});
});
</pre>
</source>




== To Do List Example ==
== To Do List Example ==
==== HTML ====
==== HTML ====
<pre>
<source lang="html4strict">
<h2>To Do</h2>
<h2>To Do</h2>
<form name="checkListForm">
<form name="checkListForm">
Line 54: Line 54:
<br/>
<br/>
<div id="list"></div>
<div id="list"></div>
</pre>
</source>


==== script.js ====
==== script.js ====
<pre>
<source lang="javascript">
$(document).ready(function(){
$(document).ready(function(){
     $('#button').click(function(){
     $('#button').click(function(){
Line 67: Line 67:
     });
     });
});
});
</pre>
</source>

Revision as of 18:54, 21 April 2015

http://learn.jquery.com/


Example

HTML

<html><head><title>Button Magic</title>
    <link rel='stylesheet' type='text/css' href='stylesheet.css'/>
    <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()
    });
});