简体   繁体   中英

Control Chrome Web Inspector in Javascript console?

Is there anyway to control the Chrome Web Inspector in the JavaScript console?

For example, say I wanted to select an element (#test_element) in the "Elements" panel. Normally I right click on the element and choose "Inspect Element".

I'd like to do this in the JavaScript console, using something like:

webInspector.inspectElement("#test_element");

I'd like to be able to access the entire UI of the web inspector this way. I'm open to using extensions for this as well.

Use inspect for this purpose. Since you want to use a selector, combine it with the console's $ alias for document.querySelector . If the page overrides $ , because it uses jQuery, use $$(selector)[0] to get a reference to the DOM element (anything would do, as long as it's a reference to a DOM node).

From the Command Line Api Reference

inspect(object)

Opens and selects the specified element or object in the appropriate panel: either the Elements panel for DOM elements and the Profiles panel for JavaScript heap objects.

The following example opens the first child element of document.body in the Elements panel:

 inspect(document.body.firstChild); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM