简体   繁体   English

动态添加的生动图标的问题

[英]Problem with dynamically added Vivid icons

I wanted to use Vivid icons in my projects.我想在我的项目中使用生动的图标 It works until I want to add it dynamically.它可以工作,直到我想动态添加它。 I guess that there is no event handler to actually change <i/> tag to <svg/>.我想没有事件处理程序可以将 <i/> 标签实际更改为 <svg/>。

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <script src="https://cdn.jsdelivr.net/npm/vivid-icons@1.0.10" type="text/javascript"></script> <p>Here is a bag</p> <i data-vi="bag"></i> <p>Now add a box</p> <button id="add" type="button">Add a box</button> <script> $("#add").click(function(){ $("body").append("<p>added, but where it is?</p>"); $("body").append('<i data-vi="bag"></i>'); }); </script>

Is there a way to trigger the function in vivid-icons.js after adding and icon '<i data-vi="bag"></i>'?添加和图标'<i data-vi="bag"></i>'后,有没有办法触发vivid-icons.js中的功能?

Thanks谢谢

I am adding actual javascript here used so you can see:我在这里添加了实际使用的 javascript,以便您可以看到:

! function(a) {
    var t = {};

    function c(h) {
        if (t[h]) return t[h].exports;
        var l = t[h] = {
            i: h,
            l: !1,
            exports: {}
        };
        return a[h].call(l.exports, l, l.exports, c), l.l = !0, l.exports
    }
    c.m = a, c.c = t, c.d = function(a, t, h) {
        c.o(a, t) || Object.defineProperty(a, t, {
            enumerable: !0,
            get: h
        })
    }, c.r = function(a) {
        "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(a, Symbol.toStringTag, {
            value: "Module"
        }), Object.defineProperty(a, "__esModule", {
            value: !0
        })
    }, c.t = function(a, t) {
        if (1 & t && (a = c(a)), 8 & t) return a;
        if (4 & t && "object" == typeof a && a && a.__esModule) return a;
        var h = Object.create(null);
        if (c.r(h), Object.defineProperty(h, "default", {
                enumerable: !0,
                value: a
            }), 2 & t && "string" != typeof a)
            for (var l in a) c.d(h, l, function(t) {
                return a[t]
            }.bind(null, l));
        return h
    }, c.n = function(a) {
        var t = a && a.__esModule ? function() {
            return a.default
        } : function() {
            return a
        };
        return c.d(t, "a", t), t
    }, c.o = function(a, t) {
        return Object.prototype.hasOwnProperty.call(a, t)
    }, c.p = "", c(c.s = 1)
}([function(a) {
    a.exports = JSON.parse('dvg goes here')
}, function(a, t, c) {
    "use strict";
    c.r(t);
    let h = "48",
        l = "#FF6E6E",
        p = "#0C0058",
        i = "#FFFFFF",
        s = function(a, t) {
            for (let c in t) a.setAttribute(c, t[c])
        };
    let r, e = c(0),
        v = function() {
            r = document.querySelectorAll("[data-vi]"), r.forEach((function(a, t) {
                let c = a,
                    r = c.dataset.vi;
                e[r]; - 1 !== r.indexOf("-") && (r = r.replace(/-([a-z])/g, (function(a) {
                    return a[1].toUpperCase()
                })));
                var v = document.createElementNS("http://www.w3.org/2000/svg", "svg");
                v.innerHTML = e[r];
                let Z = v.querySelectorAll(".vi-primary"),
                    H = v.querySelectorAll(".vi-accent"),
                    V = v.querySelectorAll(".vi-prop");
                if (null != Z && null != Z && (void 0 !== l || "" !== l))
                    for (let a = 0; a < Z.length; a++) Z[a].style.fill = l;
                if (null != H && null != H && (void 0 !== p || "" !== p))
                    for (let a = 0; a < H.length; a++) H[a].style.fill = p;
                if (null != V && null != V && (void 0 !== i || "" !== i))
                    for (let a = 0; a < V.length; a++) V[a].style.fill = i;
                if (s(v, void 0 !== h && "" !== h ? {
                        width: h,
                        height: h,
                        viewBox: "0 0 48 48"
                    } : {
                        width: "48",
                        height: "48",
                        viewBox: "0 0 48 48"
                    }), null != v) {
                    if (void 0 !== c.dataset.viSize && null !== c.dataset.viSize && s(v, {
                            width: c.dataset.viSize,
                            height: c.dataset.viSize
                        }), void 0 !== c.dataset.viPrimary && null !== c.dataset.viPrimary) {
                        let a = v.querySelectorAll(".vi-primary");
                        for (let t = 0; t < a.length; t++) a[t].style.fill = c.dataset.viPrimary
                    }
                    if (void 0 !== c.dataset.viAccent && null !== c.dataset.viAccent) {
                        let a = v.querySelectorAll(".vi-accent");
                        for (let t = 0; t < a.length; t++) a[t].style.fill = c.dataset.viAccent
                    }
                    if (void 0 !== c.dataset.viProp && null !== c.dataset.viProp) {
                        let a = v.querySelectorAll(".vi-prop");
                        for (let t = 0; t < a.length; t++) a[t].style.fill = c.dataset.viProp
                    }
                    c.parentNode.replaceChild(v, c)
                } else {
                    let a = '%c Vivid Error: No icon found for data-vi="' + r + '"';
                    console.log(a, "color: #ff4646; font-weight: bold")
                }
            }))
        };
    "loading" !== document.readyState ? v() : document.addEventListener("DOMContentLoaded", (function() {
        v()
    }))
}]);

Is there a way to execute v() function?有没有办法执行 v() 函数?

This can be a bug in vivid library, but you can fetch vivid with ajax and run it every time that you add another Icon.这可能是生动库中的一个错误,但是您可以使用 ajax 获取生动并在每次添加另一个图标时运行它。

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <p>Here is a bag</p> <i data-vi="bag"></i> <p>Now add a box</p> <button id="add" type="button">Add a box</button> <script> var vivid if (!vivid) { $.get('https://cdn.jsdelivr.net/npm/vivid-icons@1.0.10', function (script) { vivid = function () { eval(script) } vivid() }) } $("#add").click(function(){ $("body").append("<p>added, but where it is?</p>"); $("body").append('<i data-vi="bag"></i>'); vivid() }); </script>

or if you want to change library by yourself, you can add window.vivid line then every time that you add another icon just run vivid() after that:或者如果您想自己更改库,您可以添加 window.vivid 行,然后每次添加另一个图标时只需运行 vivid() 之后:

"loading" !== document.readyState ? v() : document.addEventListener("DOMContentLoaded", (function() {
    v()
}))
window.vidid = v

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM