问题
Can Chrome's last inspected elements be access with javascript?
Background, trying to integrate this into a Debug.js file to be called onclick. https://stackoverflow.com/a/12596540/511438
I am trying to integrate the function provided above to execute on a clickable element.
Here is my attempt which results in undefined even though i have inspected elements prior to executing this.
$(document).on('click', "#debugelements",
function () { CompareElements(window.$0, window.$1); });
and
function CompareElements(a, b)
{
... as per the answer above.
}
i suspect $0 is inaccessable to javascript. Is there a way to do this in javascript without pasting the whole function into the Chrome console?
回答1:
I'm afraid that only Chrome Extensions have access to these variables. There is a sample extension that makes use of $0
, it's called 'Chrome Query' and is available on samples page. I took it's code, modified a little and ended up with this extension. The code is available on github. I'm not sure if it's extra useful in current form, but there is a lot of space for improvement. Table instead of a tree should be more readable, I'll try to make it happen next week.

来源:https://stackoverflow.com/questions/12611440/accessing-chromes-last-inspected-element-being-0-and-1-via-js