2

是否可以从外部 API访问命令行API?

简单示例:

HTML

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

myScript.js

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

我不想加载像 jQuery 或 Zepto 这样的外部库,因为这样的视图已经在本地加载。

4

1 回答 1

5

要回答你的问题,不。但我不认为你真的想要。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

于 2012-12-10T16:04:59.763 回答