我如何从后面的代码中将代表 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))]);
关于javascript - 将字符串作为参数传递给 Javascript 中对象的 InnerHtml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48004269/