首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.NET JavaScript函数调用代码隐藏

ASP.NET JavaScript函数调用代码隐藏
EN

Stack Overflow用户
提问于 2017-12-29 04:57:26
回答 1查看 133关注 0票数 0

问题已更新。

参考:http://www.startuppirates.org/blog/gritter/

下面是工作正常,当点击html链接“添加规则”时,它显示通知,所以相关的.js和.css正在工作。

HTML链接

代码语言:javascript
复制
<a href="#" id="add-regular">Add regular notification</a>

JavaScript

代码语言:javascript
复制
<script type="text/javascript">

    $('#add-regular').click(function () {

        var unique_id = $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: 'This is a regular notice!',
            // (string | mandatory) the text inside the notification
            text: 'This will fade out after a certain amount of time.',
            // (string | optional) the image to display on the left
            image: '/content/gritter/images/success.png',
            // (bool | optional) if you want it to fade out on its own or just sit there
            sticky: false,
            // (int | optional) the time you want it to be alive for before fading out
            time: '',
            class_name: 'gritter-info'
        });
        return false;
    });

</script>

我已经将上面的JS修改为一个带参数的函数,并试图调用它来显示来自代码隐藏(asp.net / web-forms)的通知,但它不起作用!

代码背后

代码语言:javascript
复制
Page.ClientScript.RegisterStartupScript(Me.GetType(), "return", "stickyalert('Property Upload', 'property data uploaded successfully', 0, 'success');", True)

JavaScript

代码语言:javascript
复制
<script type="text/javascript">
    function stickyalert(title, text, sticky, success) {
        var unique_id = $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: title,
            // (string | mandatory) the text inside the notification
            text: text,
            // (string | optional) the image to display on the left
            image: '/content/gritter/images/success.png',
            // (bool | optional) if you want it to fade out on its own or just sit there
            sticky: sticky,
            // (int | optional) the time you want it to be alive for before fading out
            time: '5000',
            // (string | optional) the class name you want to apply to that specific message
            class_name: 'gritter-info'
        });
        return false;
    }
</script>
EN

回答 1

Stack Overflow用户

发布于 2017-12-29 05:02:49

你最好的办法就是把你的功能放在它自己的函数里。然后,除了从单击事件调用此函数之外,还需要在页面中呈现一个脚本,以便在使用jquery api加载函数时调用该函数。

代码语言:javascript
复制
<script type="text/javascript">

    function myFunc() {
        $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: 'This is a regular notice!',
            // (string | mandatory) the text inside the notification
            text: 'This will fade out after a certain amount of time.',
            // (string | optional) the image to display on the left
            image: '/content/gritter/images/success.png',
            // (bool | optional) if you want it to fade out on its own or just sit there
            sticky: false,
            // (int | optional) the time you want it to be alive for before fading out
            time: '',
            class_name: 'gritter-info'
        });
    }

    //Call it when the page loads
    $(document).ready(function() {
        myFunc();
    });

    //Call it on the click event
    $('#add-regular').click(function () {
        myFunc();
        return false;
    });
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48014302

复制
相关文章

相似问题

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