zoukankan      html  css  js  c++  java
  • Day 45 JavaScript Window

    Day 45 JavaScript Window

    JavaScript Window - The Browser Object Model

    There are no official standards for the Browser Object Model (BOM).

    Since modern browsers have implemented (almost) the same methods and properties for JavaScript interactivity, it is often referred to, as methods and properties of the BOM.

    The Window Object

    The window object is supported by all browsers. It represents the browser's window.

    All global JavaScript objects, functions, and variables automatically become members of the window object.

    Global variables are properties of the window object.

    Global functions are methods of the window object.

    Even the document object (of the HTML DOM) is a property of the window object:

    Window Size

    Two properties can be used to determine the size of the browser window.

    Both properties return the sizes in pixels:

    • window.innerHeight - the inner height of the browser window (in pixels)

    • window.innerWidth - the inner width of the browser window (in pixels)

    Other Window Methods

    Some other methods:

    • window.open() - open a new window

    • window.close() - close the current window

    • window.moveTo() - move the current window

    • window.resizeTo() - resize the current window

    Window Location

    The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.

    he window.location object can be written without the window prefix.

    Some examples:

    • window.location.href returns the href (URL) of the current page

    • window.location.hostname returns the domain name of the web host

    • window.location.pathname returns the path and filename of the current page

    • window.location.protocol returns the web protocol used (http: or https:)

    • window.location.assign() loads a new document

    JavaScript Window History

    The window.history object can be written without the window prefix.

    To protect the privacy of the users, there are limitations to how JavaScript can access this object.

    Some methods:

    • history.back() - same as clicking back in the browser

    • history.forward() - same as clicking forward in the browser

    Timing Events

    The window object allows execution of code at specified time intervals.

    These time intervals are called timing events.

    The two key methods to use with JavaScript are:

    • setTimeout(*function, milliseconds*) Executes a function, after waiting a specified number of milliseconds.

    • setInterval(*function, milliseconds*) Same as setTimeout(), but repeats the execution of the function continuously.

    The setTimeout() and setInterval() are both methods of the HTML DOM Window object.

    The HTML DOM (Document Object Model)

    When a web page is loaded, the browser creates a Document Object Model of the page.

    The HTML DOM Document Object

    The document object represents your web page.

    If you want to access any element in an HTML page, you always start with accessing the document object.

    Below are some examples of how you can use the document object to access and manipulate HTML.


    Finding HTML Elements

    MethodDescription
    document.getElementById(id) Find an element by element id
    document.getElementsByTagName(name) Find elements by tag name
    document.getElementsByClassName(name) Find elements by class name

    Changing HTML Elements

    PropertyDescription
    element.innerHTML = new html content Change the inner HTML of an element
    element.attribute = new value Change the attribute value of an HTML element
    element.style.property = new style Change the style of an HTML element
    Method Description
    element.setAttribute(attribute, value) Change the attribute value of an HTML element

    Adding and Deleting Elements

    MethodDescription
    document.createElement(element) Create an HTML element
    document.removeChild(element) Remove an HTML element
    document.appendChild(element) Add an HTML element
    document.replaceChild(new, old) Replace an HTML element
    document.write(text) Write into the HTML output stream

    Finding HTML Elements

    Often, with JavaScript, you want to manipulate HTML elements.

    To do so, you have to find the elements first. There are several ways to do this:

    • Finding HTML elements by id

    • Finding HTML elements by tag name

    • Finding HTML elements by class name

    • Finding HTML elements by CSS selectors

    • Finding HTML elements by HTML object collections

     

     

    jQuery

    jQuery is a JavaScript Library.

    jQuery greatly simplifies JavaScript programming.

    jQuery is easy to learn.

    What is jQuery?

    jQuery is a lightweight, "write less, do more", JavaScript library.

    The purpose of jQuery is to make it much easier to use JavaScript on your website.

    jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

    jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.

    The jQuery library contains the following features:

    • HTML/DOM manipulation

    • CSS manipulation

    • HTML event methods

    • Effects and animations

    • AJAX

    • Utilities

    Tip: In addition, jQuery has plugins for almost any task out there.

    Adding jQuery to Your Web Pages

    There are several ways to start using jQuery on your web site. You can:

    • Download the jQuery library from jQuery.com

    • Include jQuery from a CDN, like Google

    jQuery Syntax

    The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s).

    Basic syntax is: $(*selector*).*action*()

    • A $ sign to define/access jQuery

    • A (selector) to "query (or find)" HTML elements

    • A jQuery action() to be performed on the element(s)

    jQuery Selectors

    jQuery selectors allow you to select and manipulate HTML element(s).

    jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.

    All selectors in jQuery start with the dollar sign and parentheses: $().

    The element Selector

    The jQuery element selector selects elements based on the element name.

    You can select all <p> elements on a page like this:

    The #id Selector

    The jQuery #*id* selector uses the id attribute of an HTML tag to find the specific element.

    An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

    To find an element with a specific id, write a hash character, followed by the id of the HTML element:

    The .class Selector

    The jQuery *.class* selector finds elements with a specific class.

    To find elements with a specific class, write a period character, followed by the name of the class:

    More Examples of jQuery Selectors

    SyntaxDescriptionExample
    $("*") Selects all elements Try it
    $(this) Selects the current HTML element Try it
    $("p.intro") Selects all <p> elements with class="intro" Try it
    $("p:first") Selects the first <p> element Try it
    $("ul li:first") Selects the first <li> element of the first <ul> Try it
    $("ul li:first-child") Selects the first <li> element of every <ul> Try it
    $("[href]") Selects all elements with an href attribute Try it
    $("a[target='_blank']") Selects all <a> elements with a target attribute value equal to "_blank" Try it
    $("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal to "_blank" Try it
    $(":button") Selects all <button> elements and <input> elements of type="button" Try it
    $("tr:even") Selects all even <tr> elements Try it
    $("tr:odd") Selects all odd <tr> elements  

    jQuery Event Methods

    What are Events?

    All the different visitors' actions that a web page can respond to are called events.

    An event represents the precise moment when something happens.

    Examples:

    • moving a mouse over an element

    • selecting a radio button

    • clicking on an element

    The term "fires/fired" is often used with events. Example: "The keypress event is fired, the moment you press a key".

    Here are some common DOM events:

    Mouse EventsKeyboard EventsForm EventsDocument/Window Events
    click keypress submit load
    dblclick keydown change resize
    mouseenter keyup focus scroll
    mouseleave   blur unload

    jQuery Syntax For Event Methods

    In jQuery, most DOM events have an equivalent jQuery method.

    $("p").click(function(){
    // action goes here!!
    });

     

  • 相关阅读:
    5.9上午
    5.4下午
    5.4上午
    5.2上午
    4.19下午
    4.18上午
    4.17下午
    4.17上午
    4.12下午
    4.13下午
  • 原文地址:https://www.cnblogs.com/fengshili666/p/14533496.html
Copyright © 2011-2022 走看看