javascript - 将字符串作为参数传递给 Javascript 中对象的 InnerHtml 属性

标签 javascript asp.net popup

我如何从后面的代码中将代表 snackbar 的div的innerhtml设置为javascript函数中的参数? 问题: 问题是我可以使用 snackbar ,但我必须手动设置其文本,例如“操作成功”。有用。但我想让它具有多功能性,因此 insidehtml 是一个变量,而不是每次设置的静态文本。 谢谢

<style>   
  #snackbar {
    visibility: hidden;
    min-width: 250px;
    margin-left: -125px;
    background-color: #333;
    color: #fff;
    text-align: center;
    border-radius: 2px;
    padding: 16px;
    position: fixed;
    z-index: 1;
    left: 50%;
    bottom: 30px;
    font-size: 17px;
}

#snackbar.show {
    visibility: visible;
    -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
    animation: fadein 0.5s, fadeout 0.5s 2.5s;
}

@-webkit-keyframes fadein {
    from {bottom: 0; opacity: 0;} 
    to {bottom: 30px; opacity: 1;}
}

@keyframes fadein {
    from {bottom: 0; opacity: 0;}
    to {bottom: 30px; opacity: 1;}
}

@-webkit-keyframes fadeout {
    from {bottom: 30px; opacity: 1;} 
    to {bottom: 0; opacity: 0;}
}

@keyframes fadeout {
    from {bottom: 30px; opacity: 1;}
    to {bottom: 0; opacity: 0;}
}
        .auto-style3 {
            margin-left: 56px;
        }
    </style>

<div><div id="snackbar"></div></div>

<script>
   var showSnack= function (txt,x) {
         x = document.getElementById("snackbar")
         x.innerHTML = txt;
        x.className = "show";
        setTimeout(function () { x.className = 
x.className.replace("show","text"); }, 4000);
    }
</script>

隐藏代码

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text",
"showSnack(Successful,this)", true);

最佳答案

有这样的事吗?不知道我是否理解..

var showSnack= function (id, text) {
    x = document.getElementById(id)
    x.innerHTML = text;
    x.className = "show";
    setTimeout(function () { 
        x.className = x.className.replace("show","text"); 
    }, 4000);
}
let variablesnacks = ['snickers', 'chips', 'coke', 'popcorn'];
showSnack('snackbar', variablesnacks[Math.floor(Math.random()*(variablesnacks.length))]);

https://jsfiddle.net/4w8et8cc/3/

关于javascript - 将字符串作为参数传递给 Javascript 中对象的 InnerHtml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48004269/

相关文章:

javascript - jQuery 函数不在条件循环中触发

javascript - 使用 Rails 4 单击按钮时显示确认对话框?

javascript - 在 chrome 扩展的事件页面中设置值事件输入

javascript - $sce.trustAsHtml 无法按预期工作

asp.net - 该页面的状态信息无效,并且可能已损坏,现在无法工作(在 Citrix 下)

java - 检查 pop 是否用 Selenium (Java) 关闭

javascript - 如何打开弹出窗口、检测其关闭以及重定向主窗口

javascript - 如何在新的 Chrome 窗口中从 AJAX 响应打开 PDF blob

c# - 是否创建了 Controller 类实例并在 ASP.NET Framework 中的每个请求中调用了相应的实例方法?

javascript - 用于验证的 ASP.NET 客户端 API