-4

我一直在玩这个。

我不是你会告诉大声笑的 javascript 向导...

无论如何,我一直在努力让我的代码兼容,同时仍然可以正常工作。

我可以使其合规,但随后脚本停止工作,如果您问我,使其合规毫无意义。

无论如何,这里是代码片段:

(function() {

    'use strict';

    tinymce.create('tinymce.plugins.Shortcodes', {

        init : function(ed, url) {
        },
        createControl : function(n, cm) {

            if(n=='Shortcodes'){
                var msb = cm.createListBox('Shortcodes', {
                     title : 'Shortcodes',
                     onselect : function(p) {

                        if(p == 'Accordions'){

                            content =  "[accordion_wrap]<br>[accordions title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[/accordion_wrap]";

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Accordions With Icons'){

                            content =  "[accordion_wrap]<br>[accordions accordionicon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions accordionicon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions accordionicon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[/accordion_wrap]";

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Toggles'){

                            content = "[toggle_wrap]<br>[toggles title=\"Title Goes Here\"]This is where some content goes...[/toggles]<br>[toggles title=\"Title Goes Here\"]This is where some content goes...[/toggles]<br>[toggles title=\"Title Goes Here\"]This is where some content goes...[/toggles]<br>[/toggle_wrap]";

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Tabs'){

                            content =  "[tab_wrap]<br>[tab title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[tab title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[tab title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[/tab_wrap]";

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Tabs With Icons'){

                            content =  "[tab_wrap]<br>[tab icon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[tab icon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[tab icon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[/tab_wrap]";

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Latest Portfolio Items'){

                            var portfoliohowmany = prompt("How many?", 4);

                            content =  '[latest_portfolio howmany="'+portfoliohowmany+'"]';

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Testimonial Slider'){

                            content =  "[testimonial_wrap]<br>[testimonial name=\"Testimonial Name Here\" company=\"Testimonial Company Name\" testimonial=\"Testimonial goes here\"]Image Goes Here...[/testimonial]<br>[testimonial name=\"Testimonial Name Here\" company=\"Testimonial Company Name\" testimonial=\"Testimonial goes here\"]Image Goes Here...[/testimonial]<br>[testimonial name=\"Testimonial Name Here\" company=\"Testimonial Company Name\" testimonial=\"Testimonial goes here\"]Image Goes Here...[/testimonial]<br>[/testimonial_wrap]";

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Pricing Tables Wrapper'){

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){

                            content =  '[pricingtable_wrap]'+selected+'[/pricingtable_wrap]';

                            }else{

                            content =  '[pricingtable_wrap][/pricingtable_wrap]';

                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Pricing Tables'){

                            var sellingcolumns = prompt("How Many Columns (Choices are: 3 or 4)", 3);
                            var sellingtitle = prompt("Package", "");
                            var sellingprice = prompt("Price", "$29");
                            var sellingunderprice = prompt("Pre Month/Per Year", "Per Month");
                            var sellingbuttonname = prompt("Buttons Name", "More Info");
                            var sellingbuttonurl = prompt("Buttons Url", "#");
                            var sellingbuttoncolor = prompt("Button Colour... (Your Choices are: green, blue, lightblue, orange, red, black)", "green");
                            var sellingfeatured = prompt("Make This Featured (Type yes to make it featured)", "no");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[pricing_table columns="'+sellingcolumns+'" title="'+sellingtitle+'" price="'+sellingprice+'" underprice="'+sellingunderprice+'" buttonname="'+sellingbuttonname+'" buttonurl="'+sellingbuttonurl+'" buttoncolor="'+sellingbuttoncolor+'" featured="'+sellingfeatured+'"]'+selected+'[/pricing_table]';
                            }else{
                                content =  '[pricing_table columns="'+sellingcolumns+'" title="'+sellingtitle+'" price="'+sellingprice+'" underprice="'+sellingunderprice+'" buttonname="'+sellingbuttonname+'" buttonurl="'+sellingbuttonurl+'" buttoncolor="'+sellingbuttoncolor+'" featured="'+sellingfeatured+'"][/pricing_table]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Team Member'){

                            var teamhowmany = prompt("How Many In A Row: (Choices: 3,4)", 3);
                            var teamname = prompt("Team Members Name", "");
                            var teamposition = prompt("Team Members Company Position (Example: Web Developer)", "");
                            var teamcontent = prompt("Very Small Paragraph About Team Member", "");
                            var teamtwitter = prompt("Twitter Url (Including http://) ... Leave blank to not show", "");
                            var teamdribbble = prompt("Dribbble Url (Including http://) ... Leave blank to not show", "");
                            var teamfacebook = prompt("Facebook Url (Including http://) ... Leave blank to not show", "");
                            var teamlinkedin = prompt("Linked In Url (Including http://) ... Leave blank to not show", "");
                            var teamgoogle = prompt("Google Plus Url (Including http://) ... Leave blank to not show", "");
                            var teaminstagram = prompt("Instagram Url (Including http://) ... Leave blank to not show", "");
                            var teamwindows = prompt("Windows Url (Including http://) ... Leave blank to not show", "");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){

                            content =  '[teammember howmany="'+teamhowmany+'" name="'+teamname+'" position="'+teamposition+'" smallcontent="'+teamcontent+'" twitter="'+teamtwitter+'" dribbble="'+teamdribbble+'" facebook="'+teamfacebook+'" linkedin="'+teamlinkedin+'" google="'+teamgoogle+'" instagram="'+teaminstagram+'" windows="'+teamwindows+'"]'+selected+'[/teammember]';

                            } else {

                            content =  '[teammember howmany="'+teamhowmany+'" name="'+teamname+'" position="'+teamposition+'" smallcontent="'+teamcontent+'" twitter="'+teamtwitter+'" dribbble="'+teamdribbble+'" facebook="'+teamfacebook+'" linkedin="'+teamlinkedin+'" google="'+teamgoogle+'" instagram="'+teaminstagram+'" windows="'+teamwindows+'"][/teammember]';

                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Clients Show'){

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[clients]'+selected+'[/clients]';

                            } else {

                                content =  '[clients][/clients]';

                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Intro Boxes'){

                            var introboxeshowmany = prompt("How Many In A Row (Choices are: 3 or 4)", 4);
                            var introboxescolor = prompt("Box Colour (Choices are: inverse, blue, red, orane, yellow)... Inverse means sites colours!", "");
                            var introboxesicon = prompt("Icon Code, Please see Font Awesome Website For Each And Every Code", "");
                            var introboxestitle = prompt("Title", "");
                            var introboxescontent = prompt("Small Content", "");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[intro_icons howmany="'+introboxeshowmany+'" color="'+introboxescolor+'" icon="'+introboxesicon+'" title="'+introboxestitle+'" content="'+introboxescontent+'"]';
                            }else{
                                content =  '[intro_icons howmany="'+introboxeshowmany+'" color="'+introboxescolor+'" icon="'+introboxesicon+'" title="'+introboxestitle+'" content="'+introboxescontent+'"]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Progessive Bars'){

                            var progressivename = prompt("Name Of Bar", "");
                            var progressivepercentage = prompt("Percentage)", 100);
                            var progressivecolor = prompt("Bar Colour (Choices are: inverse, blue, green, orange, red)... Inverse is sites colours!", "green");
                            var progressivestriped = prompt("Stiped Effect (Choices: yes, no)", "no");
                            var progressiveactive = prompt("Moving Stripes (Choices: yes, no)... If you want moving stipes, the striped effect needs a yes!", "no");

                            content =  '[progress_bar name="'+progressivename+'" percentage="'+progressivepercentage+'" color="'+progressivecolor+'" striped="'+progressivestriped+'" active="'+progressiveactive+'"]';

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Slider'){

                            content =  "[slider_wrap]<br>[slide]Image Goes Here...[/slide]<br>[slide]Image Goes Here...[/slide]<br>[slide]Image Goes Here...[/slide]<br>[/slider_wrap]";

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Featured Link'){

                            var featuredclass = prompt("Do you want to add another css class?", "");
                            var featuredbuttonurl = prompt("Button Url", "#");
                            var featuredbuttonname = prompt("Button Name", "Buy Now");
                            var featuredbackgroundcolor = prompt("Background Colour (Choices are: white, grey, light grey, dark grey, very dark grey, blue, theme color)", "blue");
                            var featuredcontent = prompt("Content", "");
                            var featuredbuttoncolor = prompt("Button Colour (Choices are: grey, blue, lightblue, green, orange, red, black)", "");

                            content =  '[featuredlink backgroundcolor='+featuredbackgroundcolor+' class="'+featuredclass+'" buttonurl="'+featuredbuttonurl+'" buttonname="'+featuredbuttonname+'" content="'+featuredcontent+'" buttoncolor="'+featuredbuttoncolor+'"]';

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Buttons'){

                            var buttonlinkurl = prompt("Button Url", "#");
                            var buttoncolor = prompt("Button Colour (Choices are: grey, blue, aqua, green, orange, red, black)... Inverse means sites colours!", "");
                            var buttonsize = prompt("Button Size (Choices are: mini, small, normal, large)", "");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[button linkurl="'+buttonlinkurl+'" color="'+buttoncolor+'" size="'+buttonsize+'"]'+selected+'[/button]';
                            }else{
                                content =  '[button linkurl="'+buttonlinkurl+'" color="'+buttoncolor+'" size="'+buttonsize+'"][/button]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Icons'){

                            var iconicon = prompt("Icon Code, Please see Font Awesome Website For Each And Every Code", "");
                            var iconsize = prompt("Icon Size (Choices are: small, normal, large, supersize)", "normal");
                            var iconposition = prompt("Icon Position (Choices: left, right)", "left");

                            content =  '[icon icon="'+iconicon+'" size="'+iconsize+'" position="'+iconposition+'"]';

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Tool Tips'){

                            var tooltipposition = prompt("Position (Choices are: top, bottom, left, right)", "right");
                            var tooltiplink = prompt("Tool tip link url", "#");
                            var tooltiptooltip = prompt("Tooltip Content", "");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[tooltip position="'+tooltipposition+'" link="'+tooltiplink+'" tooltip="'+tooltiptooltip+'"]'+selected+'[/tooltip]';
                            }else{
                                content =  '[tooltip position="'+tooltipposition+'" link="'+tooltiplink+'" tooltip="'+tooltiptooltip+'"][/tooltip]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Alerts'){

                            var alerttype = prompt("Type (Choices are: alert-success, alert-yellow, alert-info, alert-danger, alert-warning)", "");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[alert type="'+alerttype+'"]'+selected+'[/alert]';
                            }else{
                                content =  '[alert type="'+alerttype+'"][/alert]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Labels'){

                            var labeltype = prompt("Type (Choices are: grey, green, orange, red, blue, black)", "");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[label type="'+labeltype+'"]'+selected+'[/label]';
                            }else{
                                content =  '[label type="'+labeltype+'"][/label]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Badges'){

                            var badgetype = prompt("Type (Choices are: grey, green, orange, red, blue, black)", "");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[badge type="'+badgetype+'"]'+selected+'[/badge]';
                            }else{
                                content =  '[badge type="'+badgetype+'"][/badge]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Text Highlight'){

                            var heightlightcolor = prompt("Text Colour (Please use the hex colour charts!)", "#ffffff");
                            var heightlightbgcolor = prompt("Background Colour (Please use the hex colour charts!)", "#4aaaa5");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[highlight color="'+heightlightcolor+'" bgcolor="'+heightlightbgcolor+'"]'+selected+'[/highlight]';
                            }else{
                                content =  '[highlight color="'+heightlightcolor+'" bgcolor="'+heightlightbgcolor+'"][/highlight]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Small Text'){

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[small]'+selected+'[/small]';
                            }else{
                                content =  '[small][/small]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Code Tags'){

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[code]'+selected+'[/code]';
                            }else{
                                content =  '[code][/code]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Pre Tags'){

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if( selected ){
                                //If text is selected when button is clicked
                                //Wrap shortcode around it.
                                content =  '[pre]'+selected+'[/pre]';
                            }else{
                                content =  '[pre][/pre]';
                            }

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'HR Tag'){

                            content =  '[hr]';

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                        if(p == 'Clear Fix'){

                            content =  '[clear]';

                            tinymce.execCommand('mceInsertContent', false, content);

                        } // end of shortcode

                     }
                });


                // Add some menu items
                var my_shortcodes = ['Accordions','Accordions With Icons','Toggles','Tabs','Tabs With Icons','Latest Portfolio Items','Testimonial Slider','Pricing Tables Wrapper','Pricing Tables','Team Member','Clients Show','Intro Boxes','Progessive Bars','Slider','Featured Link','Buttons','Icons','Tool Tips','Alerts','Labels','Badges','Text Highlight','Small Text','Code Tags','Pre Tags','HR Tag','Clear Fix'];

                for(var i in my_shortcodes){
                    msb.add(my_shortcodes[i],my_shortcodes[i]);
                }

                return msb;
            }
            return null;
        }


    });
    tinymce.PluginManager.add('Shortcodes', tinymce.plugins.Shortcodes);
})();

最大的问题看起来像 == 应该是 === 但是当我这样做时,它们停止工作。

另一个大问题是内容没有定义,但是当我把 var 放在它前面时,它会指出内容已经定义,所以不能赢大声笑。

我认为它没有考虑到 if 语句。

另一个是间距,我觉得这很愚蠢,我这样编码是因为它易于阅读,我总是被告知间距不会影响代码。

无论如何,任何帮助都会很棒

谢谢

4

2 回答 2

1

这是您onselect使用的方法switch-case

onselect: function (p) {
    switch (p) {
    case 'Accordions':
        content = "[accordion_wrap]<br>[accordions title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[/accordion_wrap]";
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Accordions With Icons':
        content = "[accordion_wrap]<br>[accordions accordionicon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions accordionicon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions accordionicon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[/accordion_wrap]";
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Toggles':
        content = "[toggle_wrap]<br>[toggles title=\"Title Goes Here\"]This is where some content goes...[/toggles]<br>[toggles title=\"Title Goes Here\"]This is where some content goes...[/toggles]<br>[toggles title=\"Title Goes Here\"]This is where some content goes...[/toggles]<br>[/toggle_wrap]";
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Tabs':
        content = "[tab_wrap]<br>[tab title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[tab title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[tab title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[/tab_wrap]";
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Tabs With Icons':
        content = "[tab_wrap]<br>[tab icon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[tab icon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[tab icon=\"icon-globe\" title=\"Title Goes Here\"]This is where some content goes...[/tab]<br>[/tab_wrap]";
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Latest Portfolio Items':
        var portfoliohowmany = prompt("How many?", 4);
        content = '[latest_portfolio howmany="' + portfoliohowmany + '"]';
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Testimonial Slider':
        content = "[testimonial_wrap]<br>[testimonial name=\"Testimonial Name Here\" company=\"Testimonial Company Name\" testimonial=\"Testimonial goes here\"]Image Goes Here...[/testimonial]<br>[testimonial name=\"Testimonial Name Here\" company=\"Testimonial Company Name\" testimonial=\"Testimonial goes here\"]Image Goes Here...[/testimonial]<br>[testimonial name=\"Testimonial Name Here\" company=\"Testimonial Company Name\" testimonial=\"Testimonial goes here\"]Image Goes Here...[/testimonial]<br>[/testimonial_wrap]";
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Pricing Tables Wrapper':
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            content = '[pricingtable_wrap]' + selected + '[/pricingtable_wrap]';
        } else {
            content = '[pricingtable_wrap][/pricingtable_wrap]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Pricing Tables':
        var sellingcolumns = prompt("How Many Columns (Choices are: 3 or 4)", 3);
        var sellingtitle = prompt("Package", "");
        var sellingprice = prompt("Price", "$29");
        var sellingunderprice = prompt("Pre Month/Per Year", "Per Month");
        var sellingbuttonname = prompt("Buttons Name", "More Info");
        var sellingbuttonurl = prompt("Buttons Url", "#");
        var sellingbuttoncolor = prompt("Button Colour... (Your Choices are: green, blue, lightblue, orange, red, black)", "green");
        var sellingfeatured = prompt("Make This Featured (Type yes to make it featured)", "no");
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[pricing_table columns="' + sellingcolumns + '" title="' + sellingtitle + '" price="' + sellingprice + '" underprice="' + sellingunderprice + '" buttonname="' + sellingbuttonname + '" buttonurl="' + sellingbuttonurl + '" buttoncolor="' + sellingbuttoncolor + '" featured="' + sellingfeatured + '"]' + selected + '[/pricing_table]';
        } else {
            content = '[pricing_table columns="' + sellingcolumns + '" title="' + sellingtitle + '" price="' + sellingprice + '" underprice="' + sellingunderprice + '" buttonname="' + sellingbuttonname + '" buttonurl="' + sellingbuttonurl + '" buttoncolor="' + sellingbuttoncolor + '" featured="' + sellingfeatured + '"][/pricing_table]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Team Member':
        var teamhowmany = prompt("How Many In A Row: (Choices: 3,4)", 3);
        var teamname = prompt("Team Members Name", "");
        var teamposition = prompt("Team Members Company Position (Example: Web Developer)", "");
        var teamcontent = prompt("Very Small Paragraph About Team Member", "");
        var teamtwitter = prompt("Twitter Url (Including http://) ... Leave blank to not show", "");
        var teamdribbble = prompt("Dribbble Url (Including http://) ... Leave blank to not show", "");
        var teamfacebook = prompt("Facebook Url (Including http://) ... Leave blank to not show", "");
        var teamlinkedin = prompt("Linked In Url (Including http://) ... Leave blank to not show", "");
        var teamgoogle = prompt("Google Plus Url (Including http://) ... Leave blank to not show", "");
        var teaminstagram = prompt("Instagram Url (Including http://) ... Leave blank to not show", "");
        var teamwindows = prompt("Windows Url (Including http://) ... Leave blank to not show", "");
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            content = '[teammember howmany="' + teamhowmany + '" name="' + teamname + '" position="' + teamposition + '" smallcontent="' + teamcontent + '" twitter="' + teamtwitter + '" dribbble="' + teamdribbble + '" facebook="' + teamfacebook + '" linkedin="' + teamlinkedin + '" google="' + teamgoogle + '" instagram="' + teaminstagram + '" windows="' + teamwindows + '"]' + selected + '[/teammember]';
        } else {
            content = '[teammember howmany="' + teamhowmany + '" name="' + teamname + '" position="' + teamposition + '" smallcontent="' + teamcontent + '" twitter="' + teamtwitter + '" dribbble="' + teamdribbble + '" facebook="' + teamfacebook + '" linkedin="' + teamlinkedin + '" google="' + teamgoogle + '" instagram="' + teaminstagram + '" windows="' + teamwindows + '"][/teammember]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Clients Show':
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[clients]' + selected + '[/clients]';
        } else {
            content = '[clients][/clients]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Intro Boxes':
        var introboxeshowmany = prompt("How Many In A Row (Choices are: 3 or 4)", 4);
        var introboxescolor = prompt("Box Colour (Choices are: inverse, blue, red, orane, yellow)... Inverse means sites colours!", "");
        var introboxesicon = prompt("Icon Code, Please see Font Awesome Website For Each And Every Code", "");
        var introboxestitle = prompt("Title", "");
        var introboxescontent = prompt("Small Content", "");
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[intro_icons howmany="' + introboxeshowmany + '" color="' + introboxescolor + '" icon="' + introboxesicon + '" title="' + introboxestitle + '" content="' + introboxescontent + '"]';
        } else {
            content = '[intro_icons howmany="' + introboxeshowmany + '" color="' + introboxescolor + '" icon="' + introboxesicon + '" title="' + introboxestitle + '" content="' + introboxescontent + '"]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Progessive Bars':
        var progressivename = prompt("Name Of Bar", "");
        var progressivepercentage = prompt("Percentage)", 100);
        var progressivecolor = prompt("Bar Colour (Choices are: inverse, blue, green, orange, red)... Inverse is sites colours!", "green");
        var progressivestriped = prompt("Stiped Effect (Choices: yes, no)", "no");
        var progressiveactive = prompt("Moving Stripes (Choices: yes, no)... If you want moving stipes, the striped effect needs a yes!", "no");
        content = '[progress_bar name="' + progressivename + '" percentage="' + progressivepercentage + '" color="' + progressivecolor + '" striped="' + progressivestriped + '" active="' + progressiveactive + '"]';
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Slider':
        content = "[slider_wrap]<br>[slide]Image Goes Here...[/slide]<br>[slide]Image Goes Here...[/slide]<br>[slide]Image Goes Here...[/slide]<br>[/slider_wrap]";
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Featured Link':
        var featuredclass = prompt("Do you want to add another css class?", "");
        var featuredbuttonurl = prompt("Button Url", "#");
        var featuredbuttonname = prompt("Button Name", "Buy Now");
        var featuredbackgroundcolor = prompt("Background Colour (Choices are: white, grey, light grey, dark grey, very dark grey, blue, theme color)", "blue");
        var featuredcontent = prompt("Content", "");
        var featuredbuttoncolor = prompt("Button Colour (Choices are: grey, blue, lightblue, green, orange, red, black)", "");
        content = '[featuredlink backgroundcolor=' + featuredbackgroundcolor + ' class="' + featuredclass + '" buttonurl="' + featuredbuttonurl + '" buttonname="' + featuredbuttonname + '" content="' + featuredcontent + '" buttoncolor="' + featuredbuttoncolor + '"]';
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Buttons':
        var buttonlinkurl = prompt("Button Url", "#");
        var buttoncolor = prompt("Button Colour (Choices are: grey, blue, aqua, green, orange, red, black)... Inverse means sites colours!", "");
        var buttonsize = prompt("Button Size (Choices are: mini, small, normal, large)", "");
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[button linkurl="' + buttonlinkurl + '" color="' + buttoncolor + '" size="' + buttonsize + '"]' + selected + '[/button]';
        } else {
            content = '[button linkurl="' + buttonlinkurl + '" color="' + buttoncolor + '" size="' + buttonsize + '"][/button]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Icons':
        var iconicon = prompt("Icon Code, Please see Font Awesome Website For Each And Every Code", "");
        var iconsize = prompt("Icon Size (Choices are: small, normal, large, supersize)", "normal");
        var iconposition = prompt("Icon Position (Choices: left, right)", "left");
        content = '[icon icon="' + iconicon + '" size="' + iconsize + '" position="' + iconposition + '"]';
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Tool Tips':
        var tooltipposition = prompt("Position (Choices are: top, bottom, left, right)", "right");
        var tooltiplink = prompt("Tool tip link url", "#");
        var tooltiptooltip = prompt("Tooltip Content", "");
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[tooltip position="' + tooltipposition + '" link="' + tooltiplink + '" tooltip="' + tooltiptooltip + '"]' + selected + '[/tooltip]';
        } else {
            content = '[tooltip position="' + tooltipposition + '" link="' + tooltiplink + '" tooltip="' + tooltiptooltip + '"][/tooltip]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Alerts':
        var alerttype = prompt("Type (Choices are: alert-success, alert-yellow, alert-info, alert-danger, alert-warning)", "");
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[alert type="' + alerttype + '"]' + selected + '[/alert]';
        } else {
            content = '[alert type="' + alerttype + '"][/alert]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Labels':
        var labeltype = prompt("Type (Choices are: grey, green, orange, red, blue, black)", "");
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[label type="' + labeltype + '"]' + selected + '[/label]';
        } else {
            content = '[label type="' + labeltype + '"][/label]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Badges':
        var badgetype = prompt("Type (Choices are: grey, green, orange, red, blue, black)", "");
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[badge type="' + badgetype + '"]' + selected + '[/badge]';
        } else {
            content = '[badge type="' + badgetype + '"][/badge]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Text Highlight':
        var heightlightcolor = prompt("Text Colour (Please use the hex colour charts!)", "#ffffff");
        var heightlightbgcolor = prompt("Background Colour (Please use the hex colour charts!)", "#4aaaa5");
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[highlight color="' + heightlightcolor + '" bgcolor="' + heightlightbgcolor + '"]' + selected + '[/highlight]';
        } else {
            content = '[highlight color="' + heightlightcolor + '" bgcolor="' + heightlightbgcolor + '"][/highlight]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Small Text':
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[small]' + selected + '[/small]';
        } else {
            content = '[small][/small]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Code Tags':
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[code]' + selected + '[/code]';
        } else {
            content = '[code][/code]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Pre Tags':
        selected = tinyMCE.activeEditor.selection.getContent();
        if (selected) {
            //If text is selected when button is clicked
            //Wrap shortcode around it.
            content = '[pre]' + selected + '[/pre]';
        } else {
            content = '[pre][/pre]';
        }
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'HR Tag':
        content = '[hr]';
        tinymce.execCommand('mceInsertContent', false, content);
        break;

    case 'Clear Fix':
        content = '[clear]';
        tinymce.execCommand('mceInsertContent', false, content);
        break;
    }
}

我也摆脱了所有那些不必要的空行。它可能包含一些语法错误,但我相信您可以自己修复这些错误。

这不应该破坏您的代码,它与您的 if 语句基本相同,但不需要每次都查看变量名和==或运算符。===

于 2013-08-06T08:47:42.820 回答
1

开始

为了将来参考,请务必遵循 Stack Overflow 关于提问的指南。不是因为我们(社区)陷入困境或想让人们感到困难,而是因为它可以帮助我们帮助您。向我们扔一堆代码,然后基本上说“修复这个!” 就像走进医生办公室说“让我完整!”

如果没有详细信息,我们无法为您提供帮助,而且您在描述问题时越具体越好。

话虽如此,在我的脑海中,我试图摆脱困境并专注于我想要解决的一个具体的潜在问题,因为这对于刚学习 javascript 的人来说并不明显:

最大的问题看起来像 == 应该是 === 但是当我这样做时,它们停止工作。

我采取了一些创造性的自由并将其翻译成:

当我将它们更改为 '===' 时,为什么我的平等检查失败?

在 javascript 中,有两种不同的比较运算符用于确定相等/身份。

他们是身份运算符:

===

和相等运算符:

==

他们的行为非常不同。相等运算符确实类型强制,这可能会导致有趣的、非预期的行为,而身份运算符要求操作数具有相同的类型作为先决条件。一个经常发生这种情况的地方是字符串与字符串对象。

来自不同的背景,或者是编程新手,您可能会认为:

var a = "foo";
var b = new String("foo");

做同样的事。没有。不在 Javascript 中。a是 类型string并且b是 类型object。你问为什么这很重要?考虑以下示例:

a == "foo"  // 1. evaluates to true
a === "foo" // 2. evaluates to true because `a` and "foo" are both of the same type "string"
b == "foo" //  2. evaluates to true because b is coerced from type Object to type String and its value is then compared to "foo" 
b === "foo"// 3. evaluates to false because b is type Object and "foo" is type String

因此,这归结为您在 Javascript 中确定相等性和确定身份是两个完全不同的事情。但是,一般来说,你应该坚持使用===来检查平等和身份。它使您的代码更加可靠和可预测,通常是一个好主意。在极少数情况下,您希望使用某些东西是真的,==而使用是假的===。尤其是当你刚刚学习的时候。

我没有背景tinymce,所以我不知道参数有type什么或任何其他参数,但黑暗中的一个刺实际上是一个,所以当你使用相等运算符进行比较时,它被强制并评估为是的,当您将其更改为识别运算符时,它当然会失败。我对您的第一个建议是将所有相等运算符更改为身份运算符,并尝试调用which 将返回 的值,如果确实如此的话。所以,像这样:pnpString Object==========.valueOf()pStringString Object

if(p.valueOf() === 'Accordions'){

    content =  "[accordion_wrap]<br>[accordions title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[accordions title=\"Title Goes Here\"]This is where some content goes...[/accordions]<br>[/accordion_wrap]";

    tinymce.execCommand('mceInsertContent', false, content);

 } // end of shortcode

但是你真的应该进一步教育自己==vs. ===.

关于这个有很多关于 SO 的问题,并且互联网上有很多资源可以比我更详细地解释这个问题,所以我将在这里列出一些:

我在 JavaScript 比较中使用哪个等于运算符 (== vs ===) 是否重要?

比较运算符 - JavaScript | MDN

等式运算符(JavaScript:权威指南,第 4 版)

好吧,这就是我为你准备的一切。我希望它有所帮助。如果您对我在这里写的内容有任何疑问,请随时发表评论,我一定会回复您。

快乐编码!

于 2013-08-06T08:53:12.973 回答