Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.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 如何运行2个js函数_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何运行2个js函数

Javascript 如何运行2个js函数,javascript,jquery,html,Javascript,Jquery,Html,我有两个功能,我正试图运行,一个接一个。出于某种原因,它们都同时运行,但第二个无法正确加载。有没有办法先运行第一个函数,然后再运行第二个函数 //run this first $('#abc').click(function() { $('.test1').show(); return false; }); //run this second (function ($) { "use strict"; // A nice closure for o

我有两个功能,我正试图运行,一个接一个。出于某种原因,它们都同时运行,但第二个无法正确加载。有没有办法先运行第一个函数,然后再运行第二个函数

//run this first
$('#abc').click(function() {
     $('.test1').show();
     return false;
  });  


//run this second
(function ($) {
    "use strict";
    // A nice closure for our definitions
    function getjQueryObject(string) {
        // Make string a vaild jQuery thing
        var jqObj = $("");
        try {
            jqObj = $(string)
                .clone();
        } catch (e) {
            jqObj = $("<span />")
                .html(string);
        }
        return jqObj;
    }

    function printFrame(frameWindow, content, options) {
        // Print the selected window/iframe
        var def = $.Deferred();
        try {
            frameWindow = frameWindow.contentWindow || frameWindow.contentDocument || frameWindow;
            var wdoc = frameWindow.document || frameWindow.contentDocument || frameWindow;
            if(options.doctype) {
                wdoc.write(options.doctype);
            }
            wdoc.write(content);
            wdoc.close();
            var printed = false;
            var callPrint = function () {
                if(printed) {
                    return;
                }
                // Fix for IE : Allow it to render the iframe
                frameWindow.focus();
                try {
                    // Fix for IE11 - printng the whole page instead of the iframe content
                    if (!frameWindow.document.execCommand('print', false, null)) {
                        // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891
                        frameWindow.print();
                    }
                    // focus body as it is losing focus in iPad and content not getting printed
                    $('body').focus();
                } catch (e) {
                    frameWindow.print();
                }
                frameWindow.close();
                printed = true;
                def.resolve();
            }
            // Print once the frame window loads - seems to work for the new-window option but unreliable for the iframe
            $(frameWindow).on("load", callPrint);
            // Fallback to printing directly if the frame doesn't fire the load event for whatever reason
            setTimeout(callPrint, options.timeout);
        } catch (err) {
            def.reject(err);
        }
        return def;
    }

    function printContentInIFrame(content, options) {
        var $iframe = $(options.iframe + "");
        var iframeCount = $iframe.length;
        if (iframeCount === 0) {
            // Create a new iFrame if none is given
            $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>')
                .prependTo('body')
                .css({
                    "position": "absolute",
                    "top": -999,
                    "left": -999
                });
        }
        var frameWindow = $iframe.get(0);
        return printFrame(frameWindow, content, options)
            .done(function () {
                // Success
                setTimeout(function () {
                    // Wait for IE
                    if (iframeCount === 0) {
                        // Destroy the iframe if created here
                        $iframe.remove();
                    }
                }, 1000);
            })
            .fail(function (err) {
                // Use the pop-up method if iframe fails for some reason
                console.error("Failed to print from iframe", err);
                printContentInNewWindow(content, options);
            })
            .always(function () {
                try {
                    options.deferred.resolve();
                } catch (err) {
                    console.warn('Error notifying deferred', err);
                }
            });
    }

    function printContentInNewWindow(content, options) {
        // Open a new window and print selected content
        var frameWindow = window.open();
        return printFrame(frameWindow, content, options)
            .always(function () {
                try {
                    options.deferred.resolve();
                } catch (err) {
                    console.warn('Error notifying deferred', err);
                }
            });
    }

    function isNode(o) {
        /* http://stackoverflow.com/a/384380/937891 */
        return !!(typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string");
    }
    $.print = $.fn.print = function () {
        // Print a given set of elements
        var options, $this, self = this;
        // console.log("Printing", this, arguments);
        if (self instanceof $) {
            // Get the node if it is a jQuery object
            self = self.get(0);
        }
        if (isNode(self)) {
            // If `this` is a HTML element, i.e. for
            // $(selector).print()
            $this = $(self);
            if (arguments.length > 0) {
                options = arguments[0];
            }
        } else {
            if (arguments.length > 0) {
                // $.print(selector,options)
                $this = $(arguments[0]);
                if (isNode($this[0])) {
                    if (arguments.length > 1) {
                        options = arguments[1];
                    }
                } else {
                    // $.print(options)
                    options = arguments[0];
                    $this = $("html");
                }
            } else {
                // $.print()
                $this = $("html");
            }
        }
        // Default options
        var defaults = {
            globalStyles: true,
            mediaPrint: false,
            stylesheet: null,
            noPrintSelector: ".no-print",
            iframe: true,
            append: null,
            prepend: null,
            manuallyCopyFormValues: true,
            deferred: $.Deferred(),
            timeout: 750,
            title: null,
            doctype: '<!doctype html>'
        };
        // Merge with user-options
        options = $.extend({}, defaults, (options || {}));
        var $styles = $("");
        if (options.globalStyles) {
            // Apply the stlyes from the current sheet to the printed page
            $styles = $("style, link, meta, base, title");
        } else if (options.mediaPrint) {
            // Apply the media-print stylesheet
            $styles = $("link[media=print]");
        }
        if (options.stylesheet) {
            // Add a custom stylesheet if given
            $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">'));
        }
        // Create a copy of the element to print
        var copy = $this.clone();
        // Wrap it in a span to get the HTML markup string
        copy = $("<span/>")
            .append(copy);
        // Remove unwanted elements
        copy.find(options.noPrintSelector)
            .remove();
        // Add in the styles
        copy.append($styles.clone());
        // Update title
        if (options.title) {
            var title = $("title", copy);
            if (title.length === 0) {
                title = $("<title />");
                copy.append(title);                
            }
            title.text(options.title);            
        }
        // Appedned content
        copy.append(getjQueryObject(options.append));
        // Prepended content
        copy.prepend(getjQueryObject(options.prepend));
        if (options.manuallyCopyFormValues) {
            // Manually copy form values into the HTML for printing user-modified input fields
            // http://stackoverflow.com/a/26707753
            copy.find("input")
                .each(function () {
                    var $field = $(this);
                    if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) {
                        if ($field.prop("checked")) {
                            $field.attr("checked", "checked");
                        }
                    } else {
                        $field.attr("value", $field.val());
                    }
                });
            copy.find("select").each(function () {
                var $field = $(this);
                $field.find(":selected").attr("selected", "selected");
            });
            copy.find("textarea").each(function () {
                // Fix for https://github.com/DoersGuild/jQuery.print/issues/18#issuecomment-96451589
                var $field = $(this);
                $field.text($field.val());
            });
        }
        // Get the HTML markup string
        var content = copy.html();
        // Notify with generated markup & cloned elements - useful for logging, etc
        try {
            options.deferred.notify('generated_markup', content, copy);
        } catch (err) {
            console.warn('Error notifying deferred', err);
        }
        // Destroy the copy
        copy.remove();
        if (options.iframe) {
            // Use an iframe for printing
            try {
                printContentInIFrame(content, options);
            } catch (e) {
                // Use the pop-up method if iframe fails for some reason
                console.error("Failed to print from iframe", e.stack, e.message);
                printContentInNewWindow(content, options);
            }
        } else {
            // Use a new window for printing
            printContentInNewWindow(content, options);
        }
        return this;
    };
})(jQuery);
//先运行这个
$('#abc')。单击(函数(){
$('.test1').show();
返回false;
});  
//跑这一秒
(函数($){
“严格使用”;
//对于我们的定义来说,这是一个很好的结尾
函数getjQueryObject(字符串){
//使字符串成为有效的jQuery对象
var jqObj=$(“”);
试一试{
jqObj=$(字符串)
.clone();
}捕获(e){
jqObj=$(“”)
.html(字符串);
}
返回jqObj;
}
函数printFrame(框架窗口、内容、选项){
//打印所选窗口/iframe
var def=$.Deferred();
试一试{
frameWindow=frameWindow.contentWindow | | frameWindow.contentDocument | | frameWindow;
var wdoc=frameWindow.document | | frameWindow.contentDocument | | frameWindow;
if(options.doctype){
write(options.doctype);
}
wdoc.write(内容);
wdoc.close();
var=false;
var callPrint=函数(){
如果(打印){
返回;
}
//修复IE:允许它渲染iframe
frameWindow.focus();
试一试{
//修复IE11-打印整个页面而不是iframe内容
如果(!frameWindow.document.execCommand('print',false,null)){
//document.execCommand如果失败,则返回false-http://stackoverflow.com/a/21336448/937891
frameWindow.print();
}
//关注主体,因为它在iPad中失去焦点,内容无法打印
$('body').focus();
}捕获(e){
frameWindow.print();
}
frameWindow.close();
打印=真实;
def.resolve();
}
//框架窗口加载后打印-似乎对新窗口选项有效,但对iframe不可靠
$(frameWindow).on(“加载”,callPrint);
//如果帧由于任何原因没有触发加载事件,则返回直接打印
setTimeout(callPrint,options.timeout);
}捕捉(错误){
定义拒绝(错误);
}
返回def;
}
函数PrintContentInFrame(内容、选项){
变量$iframe=$(options.iframe+“”);
变量iframecont=$iframe.length;
如果(iframeCount==0){
//如果未给出任何iFrame,则创建新iFrame
$iframe=$('')
.prependTo(“正文”)
.css({
“位置”:“绝对”,
“顶部”:-999,
“左”:-999
});
}
var frameWindow=$iframe.get(0);
返回打印框(框架窗口、内容、选项)
.done(函数(){
//成功
setTimeout(函数(){
//等我
如果(iframeCount==0){
//如果在此处创建,请销毁iframe
$iframe.remove();
}
}, 1000);
})
.失败(功能(错误){
//如果iframe因某种原因失败,请使用弹出式方法
console.error(“无法从iframe打印”,err);
printContentInNewWindow(内容、选项);
})
.always(函数(){
试一试{
options.deferred.resolve();
}捕捉(错误){
console.warn('通知延迟的错误',err);
}
});
}
函数printContentInNewWindow(内容、选项){
//打开新窗口并打印所选内容
var frameWindow=window.open();
返回打印框(框架窗口、内容、选项)
.always(函数(){
试一试{
options.deferred.resolve();
}捕捉(错误){
console.warn('通知延迟的错误',err);
}
});
}
函数isNode(o){
/* http://stackoverflow.com/a/384380/937891 */
return!!(typeof Node==“object”?o节点实例:o&&typeof o==“object”&&typeof o.nodeType==“number”&&typeof o.nodeName==“string”);
}
$.print=$.fn.print=函数(){
//打印给定的元素集
var期权,$this,self=this;
//log(“打印”、此、参数);
if(自我实例为$){
//如果节点是jQuery对象,则获取该节点
self=self.get(0);
}
if(isNode(self)){
//如果`this`是HTML元素,即
//$(选择器).print()
$this=$(self);
如果(arguments.length>0){
选项=参数[0];
}
}否则{
如果(arguments.length>0){
//$.print(选择器、选项)
$this=$(参数[0]);
if(isNode($this[0])){
如果(arguments.length>1){
选项=参数[1];
}
}否则{
//$.print(可选)
选项=参数[0];
$this=$(“html”);
}
}否则{
//$.print()
$this=$(“html”);
}
}
//默认选项
var默认值={
环球时尚:没错,
mediaPrint:false,
样式表:null,
<div id="test">
<button id="abc" class="btn" onclick="jQuery.print(#test1)"></button>
</div>
function printDiv(selector) {
    $(selector).show();
    window.setTimeout(function () {
        jQuery.print(selector);
    }, 1);
}
<div id="test">
    <button id="abc" class="btn" onclick="printDiv('#test1')"</button>
</div>