簡體   English   中英

是否可以從外部javascript訪問控制台命令行API(例如Firebug或Chrome Inspector控制台的$$和traceAll)?

[英]Can I access the console Command line API (like $$ and traceAll from Firebug or Chrome Inspector Console) from an external javascript?

是否可以從外部api訪問命令行Api

簡單的例子:

HTML

  <div id="myDiv"></div>
  <script src="myScript.js"></script>

myScript.js

$$('#myDiv').textContent = 'this will not work';

我不想加載像jQuery或Zepto這樣的外部庫,因為這樣的視圖已經在本地加載了。

要回答您的問題,不。 但是我不認為你真的想要。 API可能會更改,從而破壞您的代碼。 如果您只需要查詢選擇器。 我認為您最好使用MDN上的代碼段。

function $ (selector, el) {
    if (!el) {el = document;}
    return el.querySelector(selector);
}
function $$ (selector, el) {
    if (!el) {el = document;}
    return el.querySelectorAll(selector);
    // Note: the returned object is a NodeList.
    // If you'd like to convert it to a Array for convenience, use this instead:
    // return Array.prototype.slice.call(el.querySelectorAll(selector));
}
alert($('#myID').id);

Document.querySelector

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM