<分区>
标签 javascript html css
所以基本上如果我有 "this.that"
我希望它变成 ['this', 'that']
但 "this.(' .that')"
或 'this.(".that")'
变为 ['this', '(".that")']
string.split() 是否可行?
最佳答案
basically i am trying to write a switch statement to turn jquery into js so if you wrote $('p').hide() that would hide all the p tags but if you wrote $('.p').hide() that would hide all of the .p classes
您似乎不想使用 jQuery,而您正在尝试编写一个解析器,将 jQuery 代码转换为相应的 DOM API。这可能会导致灾难。创建一个以某种方式像 jQuery 一样工作的简单构造函数更有意义。像这样的东西:
function $(selector) {
if (!(this instanceof $)) {
return new $(selector);
}
this.length = 0;
if (typeof selector === 'undefined') {
return this;
}
var q = document.querySelectorAll(selector);
for (var i = 0, l = q.length; i < l; i++) {
this[i] = q[i];
}
this.length = q.length;
return this;
}
$.prototype.each = function(cb) {
for (var i = 0, l = this.length; i < l; i++) {
cb.apply(this[i], [i, this[i]]);
}
return this;
}
$.prototype.hide = function() {
return this.each(function(i, el) {
el.style.display = 'none';
});
}
$
函数接受 document.querySelectorAll
的所有选择器接受。
关于Javascript 分割字符串在 .但不是 。引号后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52451917/