标签:
Every modern web browser includes a powerful suite of developer tools. These tools do a range of things, from inspecting currently-loaded HTML, CSS and JavaScript to showing which assets the page has requested and how long they took to load. This article explains how to use the basic functions of your browser‘s devtools.
Note: Before you run through the examples below, open the Beginner‘s example sitethat we built during the Getting started with the Web article series. You should have this open as you follow the steps below.
The devtools live inside your browser in a subwindow that looks roughly like this:
How do you pull it up? Three ways:
The devtools usually open by default to the inspector, which looks something like the below screenshot. This tool allows you to see what the HTML on your page looks like at runtime, as well as what CSS is applied to each element on the page. It also allows you to instantly modify the HTML and CSS and see the results of your changes reflected live in the browser viewport.
If you don‘t see the inspector,
For a start, try right-clicking (Ctrl-clicking) an HTML element in the DOM inspector and look at the context menu. The menu options vary among browsers, but the important ones are mostly the same:
Try editing some of your DOM now. Double-click an element, or right-click it and chooseEdit as HTML from the context menu. You can make any changes you‘d like, but you cannot save your changes.
By default, the CSS editor displays the CSS rules applied to the currently selected element:
These features are especially handy:
You‘ll notice a number of clickable tabs at the top of the CSS Viewer:
Find more out about the Inspector in different browsers:
The JavaScript console is an incredibly useful tool for debugging JavaScript that isn‘t working as expected. It allows you to run lines of JavaScript against the page currently loaded in the browser, and reports the errors encountered as the browser tries to execute your code. To access the console in any browser, just press the Console button. (In Internet Explorer, press Ctrl + 2.) This will give you a window like the following:
To see what happens, try entering the following snippets of code into the console one by one (and then pressing Enter):
alert(‘hello!‘); document.querySelector(‘html‘).style.backgroundColor = ‘purple‘; var myImage = document.createElement(‘img‘); myImage.setAttribute(‘src‘,‘https://farm4.staticflickr.com/3455/3372925208_e1f2aae4e3_b.jpg‘); document.querySelector(‘h1‘).appendChild(myImage);
Now try entering the following incorrect versions of the code and see what you get.
alert(‘hello!); document.cheeseSelector(‘html‘).style.backgroundColor = ‘purple‘; var myImage = document.createElement(‘img‘); myBanana.setAttribute(‘src‘,‘https://farm4.staticflickr.com/3455/3372925208_e1f2aae4e3_b.jpg‘); document.querySelector(‘h1‘).appendChild(myImage);
You‘ll start to see the kind of errors that the browser returns. Often these errors are fairly cryptic, but it should be pretty simple to figure these problems out!
Find more out about the JavaScript console in different browsers:
Discover browser developer tools
标签:
原文地址:http://www.cnblogs.com/hephec/p/4601183.html