繁体   English   中英

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

[英]Pass String as Parameter to InnerHtml attribute of object in Javascript

我如何从后面的代码中将div的innerhtml设置为javascript函数中的一个参数,该div表示小吃店? 问题:问题是我可以使用快餐栏,但是我必须手动设置其文本,例如“操作成功”。 有用。 但是我想使其具有通用性,因此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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM