twitter-bootstrap Twitter Bootstrap 警报(通知)动态更改文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18104629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:51:40  来源:igfitidea点击:

Twitter Bootstrap alert (notification) dynamically change text

javascriptjquerytwitter-bootstrap

提问by Juto

Simple question, but I didn't succeed finding a satisfactory answer on SO.

简单的问题,但我没有成功找到关于 SO 的满意答案。

When I have a bootstrap alert:

当我有引导警报时:

<div id='alert' class='alert alert-error hide'>Alert.</div>

I want the text to be controlled using either Javascript of by jQuery, I just looked at this solution to this problem, but does it have to be so complicated? Really?

我希望使用 Javascript 或 jQuery 来控制文本,我只是看了这个问题的解决方案,但它必须如此复杂吗?真的吗?

回答by Yahya Yahyaoui

As simple as: $("#alert").text("My new Text");

就这么简单: $("#alert").text("My new Text");

回答by Juto

Referenced the solution and now came up with this:

参考了解决方案,现在想出了这个:

HTML:

HTML:

<div id='alert' class='hide'></div>

Javascript:

Javascript:

function showAlert(message) {
      $('#alert').html("<div class='alert alert-error'>"+message+"</div>");
      $('#alert').show();
    }
showAlert('variable');

Working jsfiddle.

工作jsfiddle

回答by Gathole

<input type = "button" id = "clickme" value="Click me!"/>
<div id = "alert_placeholder"></div>
<script>
bootstrap_alert = function() {}
bootstrap_alert.warning = function(message) {
            $('#alert_placeholder').html('<div class="alert"><a class="close" data-dismiss="alert">×</a><span>'+message+'</span></div>')
        }

$('#clickme').on('click', function() {
            bootstrap_alert.warning('Your text goes here');
});
</script>?