简体   繁体   English

如何使用javascript显示变量值?

[英]How to display variable value with use of javascript?

In @variable.value i have a string containing html tags, ie <b>aaa</b> .在 @variable.value 我有一个包含 html 标签的字符串,即<b>aaa</b> I want to display this text on a page - hmtl tags should not be displayed as strings but should format the text.我想在页面上显示此文本 - hmtl 标签不应显示为字符串,而应设置文本格式。

I am not convinced to use of methods: raw or html_safe.我不相信使用方法:raw 或 html_safe。

I would like to display @variable.value on show page with use of javascript.我想使用 javascript 在显示页面上显示 @variable.value。

I've tried:我试过了:

show.html:显示.html:

<script>
document.write('<%= @variable.value %>'); #-> <b>aaa</b>
</script>

but if i try:但如果我尝试:

document.write('<b>aaa</b>'); #this works, i get "aaa" bolded

您可以使用 console.log 或 someHTMLElement.innerHTML = 在控制台中获取它

following code is working (displays properly formatted text):以下代码有效(显示格式正确的文本):

<class id="content" data-text="<%= @variable.value %>"></class>
<script>
  var text = $("#content").data("text");
  document.write(text);
</script>

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

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