问题
I have some trouble in creating a selector in javascript.
This is my code:
function __(selector){
var self = {};
self.selector = selector;
if(typeof selector == 'object'){
self.element = self.selector;
}else{
self.element = document.querySelector(self.selector);
}
// make a .css method to an element
self.css = function(propval){
return Object.assign(self.element.style,propval);
}
return self;
}
And my html file
<script src="js/selector.js"></script>
<script>
window.onload = function(){
__('p').css({'color':'red'});
}
</script>
<p>Hello</p>
<p>World</p>
<p>John</p>
The code above will only apply the .css method
in the first <p>
element. It's because I only used querySelector
. Because querySelector
only selects the first element found. And querySelectorAll
selects all elements found. But when I try to change my selector to querySelectorAll
It doesnt work for me anymore.
回答1:
Well, the reason is querySelectorAll()
returns a NodeList of the selected elements and assigning CSS to a NodeList wouldn't make much of an effect
That said, essentially you need a way to handle the case of a single element and a many in the same way
From the top of my head, a simple solution could be to always use an arrays or the NodeList and forEach()
over them since both implement this method, like so:
function __(selector){
var self = {};
self.selector = selector;
if(typeof selector == 'object'){
self.elements = [self.selector];
}else{
self.elements = document.querySelectorAll(self.selector);
}
// make a .css method to an element
self.css = function(propval){
self.elements.forEach(function(element){
Object.assign(element.style, propval);
});
}
return self;
}
回答2:
I'm no expert here so this can probably be optimized, but with an array/list of objects you need to loop through each one
Updated with a polyfill so this one work on at least IE11/10/9
function __(selector){
var self = {};
self.selector = selector;
if(typeof selector == 'object'){
self.element = self.selector;
}else{
self.elements = document.querySelectorAll(self.selector);
}
// make a .css method to an element
self.css = function(propval){
if (self.elements) {
for (var i = 0; i < self.elements.length; i++) {
Object.assign(self.elements[i].style,propval);
}
return;
} else {
Object.assign(self.element.style,propval);
}
}
return self;
}
if (typeof Object.assign != 'function') {
Object.assign = function (target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
};
}
<p>Hello</p>
<p>World</p>
<p>John</p>
<div>Albert</div>
<script>
window.onload = function(){
__('p').css({'color':'red'});
__(document.querySelector('div')).css({'color':'blue'});
}
</script>
来源:https://stackoverflow.com/questions/41086336/queryselector-vs-queryselectorall