开发者

getElementsByTagName not a function?

开发者 https://www.devze.com 2023-02-02 23:08 出处:网络
I actually found this code on a previous post in relation to a question I asked, however, despite it seeming easy enough I can\'t get past doc.getElementsByTagName is not a function error.Here is the

I actually found this code on a previous post in relation to a question I asked, however, despite it seeming easy enough I can't get past doc.getElementsByTagName is not a function error. Here is the sample code:

var addCssRule = (function() {
        var addRule;

        if (typeof document.styleSheets != "undefined" && document.styleSheets) {
            addRule = function(selector, rule, doc, el) {
                var sheets = doc.styleSheets, sheet;
                if (sheets && sheets.length) {
                    sheet = sheets[sheets.length - 1];
                    if (sheet.addRule) {
                        sheet.addRule(selector, rule)
                    } else if (typeof sheet.cssText == "string") {
                        sheet.cssText = selector + " {" + rule + "}";
                    } else if (sheet.insertRule && sheet.cssRules) {
                        sheet.insertRule(selector + " {" + rule + "}", sheet.cssRules.length);
                    开发者_运维技巧}
                }
            }
        } else {
            addRule = function(selector, rule, doc, el) {
                el.appendChild(doc.createTextNode(selector + " {" + rule + "}"));
            };
        }

        return function(selector, rule, doc) {
            doc = doc || document;

            var head = doc.getElementsByTagName("head")[0];
            if (head && addRule) {
                var styleEl = doc.createElement("style");
                styleEl.type = "text/css";
                styleEl.media = "screen";
                head.appendChild(styleEl);
                addRule(selector, rule, doc, styleEl);
                styleEl = null;
            }
        };
    })();

    addCssRule("ol", "color:red", tinyMCE.Editor[0]);

I'm quite certain that the code is correct. I'm thinking I misunderstand how the tinymce variable is being passed. What do you think?


The third parameter you're passing into your function is tinyMCE.Editor[0]. Does that have the getElementsByTagName function?


Your code looks fine to me, although I do think that in your case, the last argument to addCssRule is not a DOM element. At least that's what would be causing such an error to be raised.


Problems here are

1. You do not call your function using a document as 3rd parameter - it is an editor instance. To fix this you should use addCssRule("ol", "color:red", tinyMCE.Editor[0].getDoc()); or addCssRule("ol", "color:red", tinyMCE.get(editor_id).getDoc());

2. You are reffering to the wrong document inside your function. What you do is reffering to the main windows document and not the document of the editor instances iframe (that is the one you want the css rule apply to)! You need to reffer to tinyMCE.Editor[0].getDoc() here too.

0

精彩评论

暂无评论...
验证码 换一张
取 消