简体   繁体   中英

How to display variable value with use of javascript?

In @variable.value i have a string containing html tags, ie <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.

I am not convinced to use of methods: raw or html_safe.

I would like to display @variable.value on show page with use of javascript.

I've tried:

show.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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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