Javascript 生成链接到querySelector的函数

Javascript 生成链接到querySelector的函数,javascript,Javascript,我想在没有jQuery的情况下重写这个jQuery函数 jQuery.fn.inputError = function(text) { $(this).parent('label').addClass('error').find('.title').after("<span class='text'>" + text + "</span>"); }; 变成 document.querySelector('.foo').inputError('message'

我想在没有jQuery的情况下重写这个jQuery函数

jQuery.fn.inputError = function(text) {

    $(this).parent('label').addClass('error').find('.title').after("<span class='text'>" + text + "</span>");

};
变成

document.querySelector('.foo').inputError('message');
问题是,我不完全确定如何编写一个函数,以便它可以链接到
querySelector()

出于个人喜好我不想做

inputError(document.querySelector('foo'));
如何编写自定义函数以便将其链接到querySelector

--

编辑。工作解决方案:

HTMLElement.prototype.inputError = function(text) {

  var label = this.parentNode,
      error = document.createElement("span");

  error.innerHTML = text;

  label.classList.add('test');
  label.insertBefore(error, label.firstChild);

};
在现代浏览器(和IE8)上,您可以通过添加以下函数来扩展
HTMLElement
的原型:

HTMLElement.prototype.inputError = function(msg) {
    // `this` is the element, so perhaps:
    this.innerHTML = msg;
};
例如:

HTMLElement.prototype.inputError=函数(msg){
//‘this’是元素,所以也许:
this.innerHTML=msg;
};
document.querySelector(“foo”).inputeror(“Hi-there”)
在现代浏览器(和IE8)上,您可以通过添加以下函数来扩展
HTMLElement
的原型:

HTMLElement.prototype.inputError = function(msg) {
    // `this` is the element, so perhaps:
    this.innerHTML = msg;
};
例如:

HTMLElement.prototype.inputError=函数(msg){
//‘this’是元素,所以也许:
this.innerHTML=msg;
};
document.querySelector(“foo”).inputeror(“Hi-there”)

提示:该函数应该是
文档的属性。querySelector()
返回的内容。您需要扩展节点/节点列表,或者编写一个包装器来封装节点/节点列表,以提供生成器模式效果。简言之,您可能会做jquery正在做的事情:)。那么,为什么不首先使用它呢?这可能会让您感兴趣。提示:该函数应该是
文档的属性。querySelector()
返回的任何内容。您需要扩展节点/节点列表,或者编写一个包装器来封装节点/节点列表,以提供生成器模式效果。简言之,您可能会做jquery正在做的事情:)。那么,为什么不首先使用它呢?这可能会让你感兴趣