<JavaScript>Basic
来源:互联网 发布:软考程序员有必要考吗 编辑:程序博客网 时间:2024/06/05 16:09
Learn from Codecadamy:
console.log('This is a message')
can ask JavaScript to print words to the console.Math.random()
can generate a random number between 0 and 1.Math.floor
will take a decimal number, and round down to the nearest whole number.- two types of code comments in JavaScript:
//
,/*
to begin the comment, and*/
to end the comment. - array 的相关用法参考: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
- We can link a JavaScript file to HTML by including it as the src of a
<script>
tag inside of an HTML file:<script src='js/main.js'></script>
alert
is a JavaScript function that will create a pop-up window with text inside it:alert('Hello JavaScript!');
- Document Object Model: commonly referred to as the DOM’, is the term for elements in an HTML file. Elements are any HTML code denoted by HTML tags, like
<div>
,<a>
, or<p>
:
var header = document.getElementsByClassName('example-class-name');// This would find an element like this in the HTML:<div class='example-class-name'> ... </div>
- To better interact with DOM elements, we can use a library. A library is a set of code that contains useful pre-written functions that help with certain tasks. A great library for interacting with the DOM is jQuery.
jQuery
- Since jQuery is a library of code, we need to include a link to it in html file before we can use it. Before the closing
</body>
tag, include this code:<script src='https://code.jquery.com/jquery-3.1.0.min.js'></script>
- The link to jQuery needs to be above the link to the js/main.js file, which will give main.js access to the jQuery library.
- jQuery has a built in function to check if the page is ready before it will run our code. write this code:
main
here is a callback, which means that our code will wait until the document (in other words, the DOM) is loaded, or ready.- When it is, then it will execute the main function. jQuery calls back to the main function, therefore it’s a callback.
$(document).ready(main);
We can wrap any CSS selector, like class, id, or tag, with
$('.example-class')
to select it with jQuery. For instance, if there’s an element with a class ofsupporting-text
, you could select it with$('.supporting-text')
. Another example, if an element had an id of'header'
, you could select it with$('#header')
.To make a page fade in, it must first be hidden. We can hide elements with jQuery with a function named
hide
:$('.my-selector').hide();
fadeIn
will fade an element in over a period of time in milliseconds:
$('.example-class').fadeIn(400);
- In order to make an element clickable, we need to write jQuery that listens to an element for a click event. jQuery can do this with an event listener function named
on('click')
:
$('.example-class').on('click', function() { // execute the code here when .example-class is clicked.});
- To make our projects visible when button is clicked, jQuery provides a function named
show
, which is the opposite ofhide
:
$('.example-class').show();
toggle
will hide or show an element, each time it is triggered:
$('example-class').toggle();
- We can toggle a CSS class with a jQuery function named
toggleClass
:
$('.example-class').toggleClass('active')
- We can select the specific element we clicked on with the jQuery selector
$(this)
:
('.example-class').on('click', function() { $(this).toggleClass('active'); //this will only toggle the class of the one that is clicked on});
- We can change the text of an element with the jQuery function
text
:
$('.my-selector').text('Hello world!');
- jQuery provides a method named
slideToggle
that can animate an element’s entrance and exit
$('.example-class').slideToggle(400);
阅读全文
0 0
- <JavaScript>Basic
- Some basic javascript usage
- JavaScript basic elements
- [FreeCodeCamp-JavaScript]Basic Algorithm
- FCC-Basic JavaScript
- FreeCodeCamp <Basic Javascript>
- Basic Javascript Introductions [Note taking]
- Basic JavaScript Slideshow without jQuery
- Basic and useful statements for JavaScript
- JavaScript学习笔记(一):Basic Knowledge
- [leetcode]224. Basic Calculator --JavaScript 代码
- [leetcode]227. Basic Calculator II -- JavaScript 代码
- JavaScript学习笔记_2_函数Basic
- @Basic
- Basic
- Basic
- BASIC
- @Basic
- HDOJ 1890 Robotic Sort
- [EMACS]helm-mode学习小结
- Django---URL
- zip函数用法
- 十个不错的javascript知识,也许会对你有帮助
- <JavaScript>Basic
- 调用startActivityForResult之后,onActivityResult立即响应
- Android Wi-Fi Wi-Fi联盟PMK Caching认证简介
- 【NKOJ3958/BZOJ2693】【莫比乌斯反演】jzptab
- 51Nod-1719-数值计算
- hdu 1728 逃离迷宫 普通的队列 / 优先队列
- HDU
- Linux桌面环境与命令行环境切换快捷键
- 第一篇= =