首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SweetAlert.js调用单独的javascript代码中的函数。不同范围的麻烦

使用SweetAlert.js调用单独的javascript代码中的函数。不同范围的麻烦
EN

Stack Overflow用户
提问于 2015-07-29 21:03:50
回答 1查看 1.9K关注 0票数 0

我正在制作一个网站,我有一个功能,允许用户拖放一个特定类型的文件,然后获得更多有关该文件的信息。除了一件事外,我的代码工作得很好。我有一个警报框(实际上是一个SweetAlert框),其中显示了用户想要查看的信息。我有处理文件的代码,然后生成一个json,最后生成包含三个按钮的html代码,每个按钮都有自己的函数,驻留在我的javascript中。我的问题是,我希望用户能够点击三种不同类型的按钮来做不同的事情,但是SweetAlert框在我的网站上充当一个新的html页面,并且有它自己的范围。警报框不承认我的javascript函数是存在的,所以它什么也不做。

要调用此警报,我使用ajax请求。这是调用函数的成功消息。数据是json格式化的。

代码语言:javascript
复制
    success: function(data) {
        informUser(printData(data))
    },

这是我打印json格式和按钮的javascript。为了简单起见,我没有包括一些部分,所以如果代码中没有意义,请告诉我。

代码语言:javascript
复制
function printData(data) {
    var str = ""
    str += '<p>Your file was added!</p>';
    str += "<p>You can access it at these locations: </p>";
    str += "<button onclick='changeView()'/>View all files</button>";
    str += "<button onclick='downloadCurrent()'/>Download the current file</button>";
    str += '<p>To upload this, click here: </p>';
    str +='<button onclick="addAsNewButton()">Add As New</button>';
    return str;
};

这是我的SweetAlert,它在某种程度上充当自己的javascript文件。

代码语言:javascript
复制
var informUser = function(message){
    swal({title: "Congrats on uploading a file!", html: true, text: message, type: "info" });
}; 

我读了一些关于SweetAlerts如何在其中包含函数的例子,我需要帮助打电话给我的。如果有人认为他们可以在这方面帮助我,我愿意解释任何可能需要的东西,包括我尝试过的和失败的东西。我甚至希望就如何解决这一问题提出一般性意见。这里有一个指向SweetAlert文档http://t4t5.github.io/sweetalert/的链接。

EN

回答 1

Stack Overflow用户

发布于 2015-07-30 06:25:12

我不知道您的代码是如何构造的。您必须确保该函数是可见的。

一种快速而肮脏的方式是:

代码语言:javascript
复制
window.changeView = function(){
    // your code
};
window.downloadCurrent = function(){
    // your code
};
window.addAsNewButton = function(){
    // your code
};

// ...

function printData(data) {
    var str = ""
    str += '<p>Your file was added!</p>';
    str += "<p>You can access it at these locations: </p>";
    str += "<button onclick='window.changeView()'/>View all files</button>";
    str += "<button onclick='window.downloadCurrent()'/>Download the current file</button>";
    str += '<p>To upload this, click here: </p>';
    str +='<button onclick="window.addAsNewButton()">Add As New</button>';
    return str;
};

更好的方法是使用模块模式(http://toddmotto.com/mastering-the-module-pattern/):

代码语言:javascript
复制
var yourModule = (function () {

  return {
    changeView : function(){
    };
    downloadCurrent : function(){
    };
    addAsNewButton : function(){
    };
  };

})();

// ...

function printData(data) {
    var str = ""
    str += '<p>Your file was added!</p>';
    str += "<p>You can access it at these locations: </p>";
    str += "<button onclick='window.yourModule.changeView()'/>View all files</button>";
    str += "<button onclick='window.yourModule.downloadCurrent()'/>Download the current file</button>";
    str += '<p>To upload this, click here: </p>';
    str +='<button onclick="window.yourModule.addAsNewButton()">Add As New</button>';
    return str;
};

希望能帮上忙。

编辑:

一个更好的方法是在呈现之后附加事件(使用jQuery的示例):

代码语言:javascript
复制
var informUser = function(message){
    swal({title: "Congrats on uploading a file!", html: true, text: message, type: "info" });
    $(".btn-change-view").click(function(){   });
    $(".btn-download-current").click(function(){   });
    $(".btn-add-as-new").click(function(){   });
}; 

// ...

function printData(data) {
    var str = ""
    str += '<p>Your file was added!</p>';
    str += "<p>You can access it at these locations: </p>";
    str += "<button class="btn-change-view"/>View all files</button>";
    str += "<button class="btn-download-current"/>Download the current file</button>";
    str += '<p>To upload this, click here: </p>';
    str +='<button class="btn-add-as-new">Add As New</button>';
    return str;
};

评论后的编辑:

如果只是这样,您可以尝试如下:

代码语言:javascript
复制
var informUser = function(message){
    swal({title: "Congrats on uploading a file!", html: true, text: message, type: "info" });
    $(".btn-change-view").click(changeView);
    $(".btn-download-current").click(downloadCurrent);
    $(".btn-add-as-new").click(addAsNewButton);
}; 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31711082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档