Javascript 分割字符串在 .但不是 。引号后

标签 javascript html css

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 4 年前

所以基本上如果我有 "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/

上一篇:html - Materialise textarea 标签在固定高度上不可滚动

下一篇:jquery - CSS 变量不适用于多个 SVG 渐变

相关文章:

jquery - 只高亮选中的元素,而不是它的父元素?

javascript - 在 JavaScript 中,整数在存储布局中是如何表示的?

javascript - 如何确定图像是 JavaScript 中的 URI 还是缩略图

jquery - 使用 JQuery 滚动 spy

html - <col> 宽度和填充 - IE7

html - 创建可折叠的导航栏

html - 为什么 IE 尊重标签 CSS 宽度,而不是 Firefox 或 Chrome?

javascript - Angular 6 + Universal Karma 测试导入模块

javascript - jsdom 中外部脚本的变量引用

jQuery - 根据类名删除 DIV