Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法设置可编辑div的格式_Javascript_Jquery_Html - Fatal编程技术网

Javascript 无法设置可编辑div的格式

Javascript 无法设置可编辑div的格式,javascript,jquery,html,Javascript,Jquery,Html,我正试图建立一个所见即所得编辑器,我有一个弹出窗口上的工作按钮。每当可编辑div悬停时,它都会显示一个边框,当单击该div时,一个按钮会淡出,在弹出窗口中显示按钮。我已将js代码包括在下面: $.fn.nitspopupeditor = function () { //Function to format editable items. this.each(function () { setTimeout(function () {}, 100);

我正试图建立一个所见即所得编辑器,我有一个弹出窗口上的工作按钮。每当可编辑div悬停时,它都会显示一个边框,当单击该div时,一个按钮会淡出,在弹出窗口中显示按钮。我已将js代码包括在下面:

 $.fn.nitspopupeditor = function () { //Function to format editable items.
        this.each(function () {
            setTimeout(function () {}, 100);
            var $this = $(this);
            var selected_text = null;
            var uniquid = "nits" + new Date().getTime();

            $this.attr('data-nitsselect', uniquid); //set element unique id

            function getSelected() { //get selected text
                var selection = window.getSelection();
                range = selection.getRangeAt(0);
                return range;
            }


            function waitForPasteData(context, content) { //wait for paste data
                if (context.childNodes && context.childNodes.length > 0) {
                    processPaste(context, content);
                } else {
                    that = {
                        e: context
                        , s: content
                    };
                    that.callself = function () {
                        waitForPasteData(that.e, that.s);
                    }
                    setTimeout(that.callself, 20);
                }
            }

            function getHTMLOfSelection() {
                var range;
                if (document.selection && document.selection.createRange) {
                    range = document.selection.createRange();
                    return range.htmlText;
                } else if (window.getSelection) {
                    var selection = window.getSelection();
                    if (selection.rangeCount > 0) {
                        range = selection.getRangeAt(0);
                        var clonedSelection = range.cloneContents();
                        var div = document.createElement('div');
                        div.appendChild(clonedSelection);
                        return div.innerHTML;
                    } else {
                        return '';
                    }
                } else {
                    return '';
                }
            }

            function processPaste(context, content) {
                paste_data = context.innerHTML;

                context.innerHTML = content;

                var $div = $('<div></div>');
                $div.html(paste_data);
                $(context).append($div.text());
            }

            $this.on('mouseup', function () {
                selected_text = getSelected(); //get current selected text
            });

            $this.on('paste', function (e) {
                var saved_content = $(this).html(); //manipulate paste-text
                if (e && e.clipboardData && e.clipboardData.getData) {
                    if (/text\/html/.test(e.clipboardData.types)) {
                        $(this).html(e.clipboardData.getData('text/html'));
                    } else if (/text\/plain/.test(e.clipboardData.types)) {
                        $(this).html(e.clipboardData.getData('text/plain'));
                    } else {
                        $(this).html('');
                    }
                    waitForPasteData($(this)[0], saved_content);
                    if (e.preventDefault) {
                        e.stopPropagation();
                        e.preventDefault();
                    }
                    return false;
                } else {
                    $(this).html('');
                    waitForPasteData($(this)[0], saved_content);
                    return true;
                }
            });

            $('[data-nitsedittag]').on('click', function (e) {

                var selection = window.getSelection();
                var $parent = $(selection.anchorNode.parentElement);
                if ($parent.data('nitsselect') == uniquid && $parent.is(':focus')) {

                    var tag = $(this).data('nitsedittag');
                    switch (tag) {
                        default: document.execCommand(tag);
                    }

                }
                e.preventDefault();

            });
        })
    };



    function closep() { // Function to close the popup on close button.
        $('[data-nitstextpopup]').fadeOut(400);
        $('[data-nitstextbutton]').show();
    }
    $(function () {
        var mouseX;
        var mouseY;
        var modal = false;
        $(document).mousemove(function (f) {
            mouseX = f.pageX;
            mouseY = f.pageY;
        });

        var openPopup = function (e) { //Function to open popup
            $(e).fadeIn(400);
            $('[data-nitstextpopup]').fadeIn(400).css({
                'top': mouseY
                , 'left': mouseX
            }).draggable();
            $('[data-nitstextbutton]').hide();
        };


        var closePopup = function () { // Function to close the popup
            $('[data-nitstextpopup]').fadeOut(400);
            $('[data-nitstextbutton]').show();
        };

        $('[data-nitspagelabel]').hover(function () { // to hover only editable items
            $(this).css('border', 'solid 1px #777');
        }, function () {
            $(this).css('border', 'none');
        });
        $('[data-nitspagelabel]').click(function () { //click function on editable div to get editable buttons
            $(this).attr('contenteditable', 'true');
            var labeltype = $('[data-nitspagelabel]').data("nitslabeltype");
            if (labeltype == "text") {
                if (modal == false) {
                    modal = true;
                    $('[data-nitstextbutton]').css({ //popup text editing buttons 
                        'top': mouseY
                        , 'left': mouseX
                    }).fadeIn(400).click(function (e) {
                        var popupbox = $(this).attr('href');
                        openPopup(popupbox); // opens the editing tools popup
                        $('[data-nitspagelabel]').nitspopupeditor; // formatting buttons in action
                    });
                }
            }
        });

        $(document).click(function (e) { // clicking outside closing the popup
            closePopup();
            modal = false;
            $('[data-nitstextbutton]').hide();
        });


        $('[data-nitstextpopup]').click(function (e) { // setting execption areas for closing popup
            e.stopPropagation();
        });


        $("#btnedit").click(function (e) { // setting execption areas for closing popup
            e.stopPropagation();
        });


        $("#editable").click(function (e) { // setting execption areas for closing popup
            e.stopPropagation();
        });

        $(document).keyup(function (e) { // setting esc button close popup
            if (e.keyCode == 27) {
                closePopup();
                modal = false;
                $('[data-nitstextbutton]').hide();
            }
        });

    });
$.fn.nitspopupeditor=function(){//function设置可编辑项的格式。
这个。每个(函数(){
setTimeout(函数(){},100);
var$this=$(this);
var selected_text=null;
var uniquid=“nits”+新日期().getTime();
$this.attr('data-nitsselect',uniquid);//设置元素唯一id
函数getSelected(){//获取所选文本
var selection=window.getSelection();
范围=选择。getRangeAt(0);
返回范围;
}
函数waitForPasteData(上下文、内容){//等待粘贴数据
if(context.childNodes&&context.childNodes.length>0){
processPaste(上下文、内容);
}否则{
那={
e:背景
,s:内容
};
that.callself=函数(){
waitForPasteData(即.e,即.s);
}
setTimeout(即.callself,20);
}
}
函数getHTMLOfSelection(){
var范围;
if(document.selection&&document.selection.createRange){
range=document.selection.createRange();
返回range.htmlText;
}else if(window.getSelection){
var selection=window.getSelection();
如果(selection.rangeCount>0){
范围=选择。getRangeAt(0);
var clonedSelection=range.cloneContents();
var div=document.createElement('div');
子类(克隆选择);
返回div.innerHTML;
}否则{
返回“”;
}
}否则{
返回“”;
}
}
函数processPaste(上下文、内容){
粘贴_data=context.innerHTML;
context.innerHTML=内容;
变量$div=$('');
$div.html(粘贴数据);
$(上下文).append($div.text());
}
$this.on('mouseup',函数(){
选定的文本=getSelected();//获取当前选定的文本
});
$this.on('paste',函数(e){
var saved_content=$(this).html();//操作粘贴文本
if(e&&e.clipboardData&&e.clipboardData.getData){
if(/text\/html/.test(e.clipboardData.types)){
$(this.html(e.clipboardData.getData('text/html');
}else if(/text\/plain/.test(例如clipboardData.types)){
$(this.html(e.clipboardData.getData('text/plain');
}否则{
$(this.html(“”);
}
waitForPasteData($(此)[0],已保存的内容);
如果(如默认){
e、 停止传播();
e、 预防默认值();
}
返回false;
}否则{
$(this.html(“”);
waitForPasteData($(此)[0],已保存的内容);
返回true;
}
});
$(“[data nitsedittag]”)。在('click',函数(e)上{
var selection=window.getSelection();
var$parent=$(selection.anchorNode.parentElement);
如果($parent.data('nitsselect')==uniquid&&$parent.is(':focus')){
var标记=$(this.data('nitsedittag');
开关(标签){
默认值:document.execCommand(标记);
}
}
e、 预防默认值();
});
})
};
函数closep(){//用于关闭关闭按钮时弹出窗口的函数。
$(“[data nitstextpopup]”)。淡出(400);
$('[data nitstextbutton]')。show();
}
$(函数(){
var mouseX;
var mouseY;
var模态=假;
$(文档).mousemove(函数(f){
mouseX=f.pageX;
mouseY=f.pageY;
});
var openPopup=函数(e){//打开弹出窗口的函数
美元(e).fadeIn(400美元);
$(“[data nitstextpopup]”).fadeIn(400).css({
“顶”:老鼠
“左”:鼠标
}).draggable();
$('[data nitstextbutton]')。隐藏();
};
var closePopup=function(){//关闭弹出窗口的函数
$(“[data nitstextpopup]”)。淡出(400);
$('[data nitstextbutton]')。show();
};
$(“[data nitspagelabel]”)。悬停(函数(){//仅悬停可编辑项
$(this.css('border','solid 1px#777');
},函数(){
$(this.css('border','none');
});
$(“[data nitspagelabel]”)。单击(函数(){//单击可编辑div上的函数以获取可编辑按钮
$(this.attr('contenteditable','true');
var labeltype=$('[data nitspagelabel]')。数据(“nitslabeltype”);
如果(标签类型==“文本”){
如果(模态==假){
模态=真;
$(“[data nitstextbutton]”)。css({//弹出文本编辑按钮
“顶”:老鼠
“左”:鼠标
}).fadeIn(400)。单击(功能(e){
var popubox=$(this.attr('href');
$('[data-nitsedittag]').click(function (e) {
        var selection = window.getSelection();
        var $parent = $(selection.anchorNode.parentElement);
        var $focusedElement = $("*:focus");
        var tag = $(this).data('nitsedittag');
        switch (tag) {
                case 'link':
                    var sLnk = prompt('Write the URL here', 'http:\/\/');
                    if (sLnk && sLnk != '' && sLnk != 'http://') {
                        document.execCommand('createlink', false, sLnk);
                    }
                    break;
                case 'Fontname':
                    debugger;
                    if (this[this.selectedIndex].value == 'Select Font') {
                        break;
                    } else {
                        document.execCommand(tag, false, this[this.selectedIndex].value);
                        selectedIndex = 0;
                    }

                case 'formatblock':
                    document.execCommand(tag, false, this[this.selectedIndex].value);
                    selectedIndex = 0;
                case 'fontSize':
                    document.execCommand(tag, false, this[this.selectedIndex].value);
                    selectedIndex = 0;
                default:
                    document.execCommand(tag);
                }
                e.preventDefault();

            });