【问题标题】:Pass String as Parameter to InnerHtml attribute of object in Javascript在Javascript中将字符串作为参数传递给对象的InnerHtml属性
【发布时间】:2017-12-28 08:42:06
【问题描述】:

如何从后面的代码中将代表小吃店的 div 的 innerhtml 设置为 javascript 函数中的参数? 问题: 问题是我可以使用snackbar,但我必须手动设置它的文本,比如“操作成功”。有用。但我想让它变得通用,所以 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);

【问题讨论】:

  • 我没有看到任何问题。你的代码是正确的。只需使用适当的文本调用您的函数。
  • 能否请您通过后面的代码检查。可能有问题..谢谢
  • 在您的消息周围添加单引号。 ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "showSnack('Successful',this)", true);
  • 感谢它有效。我应该将字符串包装在''中。

标签: javascript asp.net popup


【解决方案1】:

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

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/

【讨论】:

  • 谢谢。问题出在我的代码后面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2013-01-21
相关资源
最近更新 更多