将String作为参数传递给Javascript中对象的InnerHtml属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将String作为参数传递给Javascript中对象的InnerHtml属性相关的知识,希望对你有一定的参考价值。

我如何设置div的innerhtml,它代表一个快餐栏,作为javascript函数中的参数从代码背后?问题:问题是我可以使用小吃吧但我必须手动设置其文本,如“操作成功”。有用。但我想让它变得多才多艺,所以innerhtml每次都是变量而不是静态文本集。谢谢

<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/

以上是关于将String作为参数传递给Javascript中对象的InnerHtml属性的主要内容,如果未能解决你的问题,请参考以下文章

MVC - 将整个模型作为参数传递给 JavaScript 中的 Url.Action

如何通过 HtmlDocument.InvokeScript 将对象作为参数传递给 javascript

QML:如何将javascript函数作为参数传递给另一个函数

将 List<String> 作为参数传递给 postgres 函数

如何将 C# 对象作为参数传递给 Javascript 函数

尝试将 Map 作为参数传递给 Udf 时没有 typeTag 异常