我正在尝试将长字符串放入 JS 对象中。我使用的代码可以正常工作:
parent.$("#data-store").data("content[<%=n%>]" , "<%= @content[n.to_s].squish %>");
所以我的对象看起来像这样:
content[0]: "<div class="ui-wrapper ui-draggable" style="overflow: hidden; position: absolute; width: 219px; height: 194px; top: 313.31597900390625px; left: 81.32640075683594px; margin: 0px;"><img id="link0" style="position: static; margin: 0px; resize: none; zoom: 1; display: block; height: 194px; width: 219px;" src="http://i.imgur.com/5H8QhMr.png" class="ui-resizable"><div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div><img class="icon-layer-up icon-on-img" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down icon-on-img" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash1 icon-on-img" src="/assets/icon_trash.png" style="z-index: 2;"><img class="icon-copy-el icon-on-img" src="/assets/icon_copy.png" style="z-index: 2;"><img class="icon-layer-up icon-on-img" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down icon-on-img" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash1 icon-on-img" src="/assets/icon_trash.png" style="z-index: 2;"><img class="icon-copy-el icon-on-img" src="/assets/icon_copy.png" style="z-index: 2;"><img class="icon-layer-up icon-on-img" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down icon-on-img" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash1 icon-on-img" src="/assets/icon_trash.png" style="z-index: 2;"><img class="icon-copy-el icon-on-img" src="/assets/icon_copy.png" style="z-index: 2;"><img class="icon-layer-up icon-on-img" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down icon-on-img" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash1 icon-on-img" src="/assets/icon_trash.png" style="z-index: 2;"><img class="icon-copy-el icon-on-img" src="/assets/icon_copy.png" style="z-index: 2;"><img class="icon-layer-up icon-on-img" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down icon-on-img" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash1 icon-on-img" src="/assets/icon_trash.png" style="z-index: 2;"><img class="icon-copy-el icon-on-img" src="/assets/icon_copy.png" style="z-index: 2;"><img class="icon-layer-up icon-on-img" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down icon-on-img" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash1 icon-on-img" src="/assets/icon_trash.png" style="z-index: 2;"><img class="icon-copy-el icon-on-img" src="/assets/icon_copy.png" style="z-index: 2;"><img class="icon-layer-up icon-on-img" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down icon-on-img" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash1 icon-on-img" src="/assets/icon_trash.png" style="z-index: 2;"><img class="icon-copy-el icon-on-img" src="/assets/icon_copy.png" style="z-index: 2;"></div> <div class="editor ui-resizable ui-draggable" contenteditable="true" style="position: absolute; top: 124.3125px; left: 185.3229217529297px; width: 549px; height: 254px;"><h2 class="text2" style="font-size: 4em; font-family: Allura;">ddsadasd</h2><div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div><img class="icon-layer-up2 icon-on-edit" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down2 icon-on-edit" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash2 icon-on-edit" src="/assets/icon_trash.png" style="z-index: 2;"><i class="icon-move icon-on-edit" style="z-index: 2;"></i><i class="icon-font font2 icon-on-edit" style="z-index: 2;"></i><img class="icon-font-size icon-on-edit" src="/assets/font_size2.png" style="z-index: 2;"><img class="icon-layer-up2 icon-on-edit" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down2 icon-on-edit" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash2 icon-on-edit" src="/assets/icon_trash.png" style="z-index: 2;"><i class="icon-move icon-on-edit" style="z-index: 2;"></i><i class="icon-font font2 icon-on-edit" style="z-index: 2;"></i><img class="icon-font-size icon-on-edit" src="/assets/font_size2.png" style="z-index: 2;"><img class="icon-layer-up2 icon-on-edit" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down2 icon-on-edit" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash2 icon-on-edit" src="/assets/icon_trash.png" style="z-index: 2;"><i class="icon-move icon-on-edit" style="z-index: 2;"></i><i class="icon-font font2 icon-on-edit" style="z-index: 2;"></i><img class="icon-font-size icon-on-edit active" src="/assets/font_size2.png" style="z-index: 2;"><div class="fonts-size-container active" style="display: none;"> <ul class="no-edit"> <li class="no-edit" data-fontsize="8em">8 em</li> <li class="no-edit" data-fontsize="7em">7 em</li> <li class="no-edit" data-fontsize="6em">6 em</li> <li class="no-edit" data-fontsize="5em">5 em</li> <li class="no-edit" data-fontsize="4em">4 em</li> <li class="no-edit" data-fontsize="3.5em">3,5 em</li> <li class="no-edit" data-fontsize="3em">3 em</li> <li class="no-edit" data-fontsize="2.5em">2.5 em</li> <li class="no-edit" data-fontsize="2em">2 em</li> <li class="no-edit" data-fontsize="1.5em">1.5 em</li> <li class="no-edit" data-fontsize="1.3em">1.3 em</li> <li class="no-edit" data-fontsize="1.2em">1.2 em</li> <li class="no-edit" data-fontsize="1em">1 em</li> <li class="no-edit" data-fontsize="0.8em">0.8 em</li> <li class="no-edit" data-fontsize="0.6em">0.6 em</li> <li class="no-edit" data-fontsize="0.5em">0.5 em</li> </ul> </div><img class="icon-layer-up2 icon-on-edit" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down2 icon-on-edit" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash2 icon-on-edit" src="/assets/icon_trash.png" style="z-index: 2;"><i class="icon-move icon-on-edit" style="z-index: 2;"></i><i class="icon-font font2 icon-on-edit" style="z-index: 2;"></i><img class="icon-font-size icon-on-edit" src="/assets/font_size2.png" style="z-index: 2;"><div class="fonts-container" style="display: none;"> <ul class="no-edit"> <li class="no-edit" data-fontname="Abril Fatface"><img class="icon-on-edit" src="/assets/fonts/Abril-Fatface.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Alegreya Small Caps"><img class="icon-on-edit" src="/assets/fonts/Alegreya-Small-Caps.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Alfa Slab One"><img class="icon-on-edit" src="/assets/fonts/Alfa-Slab-One.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Alike"><img class="icon-on-edit" src="/assets/fonts/Alike.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Allerta Stencil"><img class="icon-on-edit" src="/assets/fonts/Allerta-Stencil.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Allura"><img class="icon-on-edit" src="/assets/fonts/Allura.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Amaranth"><img class="icon-on-edit" src="/assets/fonts/Amaranth.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Amatic SC"><img class="icon-on-edit" src="/assets/fonts/Amatic-Small-Caps.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Anonymous Pro"><img class="icon-on-edit" src="/assets/fonts/Anonymous-Pro.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Antic"><img class="icon-on-edit" src="/assets/fonts/Antic.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Anton"><img class="icon-on-edit" src="/assets/fonts/Anton.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Architects Daughter"><img class="icon-on-edit" src="/assets/fonts/Architects-Daughter.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Archivo Narrow"><img class="icon-on-edit" src="/assets/fonts/Archivo-Narrow.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Arimo"><img class="icon-on-edit" src="/assets/fonts/Arimo.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Arvo"><img class="icon-on-edit" src="/assets/fonts/Arvo.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Berkshire Swash"><img class="icon-on-edit" src="/assets/fonts/Berkshire-Swash.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Black Ops One"><img class="icon-on-edit" src="/assets/fonts/Black-Ops-One.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Cabin Sketch"><img class="icon-on-edit" src="/assets/fonts/Cabin-Sketch.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Cantora One"><img class="icon-on-edit" src="/assets/fonts/Cantora-One.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Carter One"><img class="icon-on-edit" src="/assets/fonts/Carter-One.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Chewy"><img class="icon-on-edit" src="/assets/fonts/Chewy.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Cinzel"><img class="icon-on-edit" src="/assets/fonts/Cinzel.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Clicker Script"><img class="icon-on-edit" src="/assets/fonts/Clicker-Script.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Codystar"><img class="icon-on-edit" src="/assets/fonts/Codystar.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Courgette"><img class="icon-on-edit" src="/assets/fonts/Courgette.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Coustard"><img class="icon-on-edit" src="/assets/fonts/Coustard.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Crafty Girls"><img class="icon-on-edit" src="/assets/fonts/Crafty-Girls.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Creepster"><img class="icon-on-edit" src="/assets/fonts/Creepster.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Crushed"><img class="icon-on-edit" src="/assets/fonts/Crushed.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Diplomata SC"><img class="icon-on-edit" src="/assets/fonts/Diplomata-Small-Caps.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Dr Sugiyama"><img class="icon-on-edit" src="/assets/fonts/Dr-Sugiyama.png" style="z-index: 2;"></li> <li class="no-edit" data-fontname="Droid Serif"><img class="icon-on-edit" src="/assets/fonts/Droid-Serif.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Economica"><img class="icon-on-edit" src="/assets/fonts/Economica.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Emilys Candy"><img class="icon-on-edit" src="/assets/fonts/Emilys-Candy.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Fredoka One"><img class="icon-on-edit" src="/assets/fonts/Fredoka-One.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Germania One"><img class="icon-on-edit" src="/assets/fonts/Germania-One.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Glass Antiqua"><img class="icon-on-edit" src="/assets/fonts/Glass-Antiqua.png " style="z-index: 2;"></li> <li class="no-edit" data-fontname="Glegoo"><img class="icon-on-edit" src="/assets/fonts/Glegoo.png " style="z-index: 2;"></li> </ul> </div><img class="icon-layer-up2 icon-on-edit" src="/assets/icon_layer_up.png" style="z-index: 2;"><img class="icon-layer-down2 icon-on-edit" src="/assets/icon_layer_down.png" style="z-index: 2;"><img class="icon-trash2 icon-on-edit" src="/assets/icon_trash.png" style="z-index: 2;"><i class="icon-move icon-on-edit" style="z-index: 2;"></i><i class="icon-font font2 icon-on-edit" style="z-index: 2;"></i><img class="icon-font-size icon-on-edit" src="/assets/font_size2.png" style="z-index: 2;"><div class="fonts-size-container" style="display: none;"> <ul class="no-edit"> <li class="no-edit" data-fontsize="8em">8 em</li> <li class="no-edit" data-fontsize="7em">7 em</li> <li class="no-edit" data-fontsize="6em">6 em</li> <li class="no-edit" data-fontsize="5em">5 em</li> <li class="no-edit activefont" data-fontsize="4em">4 em<i class="icon-check"></i></li> <li class="no-edit" data-fontsize="3.5em">3,5 em</li> <li class="no-edit" data-fontsize="3em">3 em</li> <li class="no-edit" data-fontsize="2.5em">2.5 em</li> <li class="no-edit" data-fontsize="2em">2 em</li> <li class="no-edit" data-fontsize="1.5em">1.5 em</li> <li class="no-edit" data-fontsize="1.3em">1.3 em</li> <li class="no-edit" data-fontsize="1.2em">1.2 em</li> <li class="no-edit" data-fontsize="1em">1 em</li> <li class="no-edit" data-fontsize="0.8em">0.8 em</li> <li class="no-edit" data-fontsize="0.6em">0.6 em</li> <li class="no-edit" data-fontsize="0.5em">0.5 em</li> </ul> </div></div>"
但是当我想通过构成 JS 对象并获取参数 @content 来生成 HTML 并且我想在 HTML 中呈现它时:
<div class="step-wrapper">
<%=
if @content.nil? || @content[n.to_s].nil?
else
@content[n.to_s].html_safe
end
%>
</div>
我得到的是用引号 "" 包裹的 HTML,因此 HTML 代码形式 @content 显示为文本。
在打印到 div 时,我应该使用哪种正则表达式从引号中解开这个对象?
带有 html_safe 的代码给了我 JS 错误,在这种情况下我无法使用它。
parent.$("#data-store").data("content[<%=n%>]" , "<%= @content[n.to_s].squish.html_safe %>");