如何使用.html()将html / ruby​​片段插入div

时间:2016-01-02 21:12:33

标签: jquery html ruby

我正在尝试插入此代码:

SCNLevelOfDetail

进入ID为id的div。我想我需要使用 Tags: <% @item.all_tags_list.each do |t| %> <span class="tag-container"> <%= link_to t, tag_path(t) %> <% if @item.tags_from(current_user).include?(t) %> <span> <%= link_to "×", '', class: "remove-tag-button", :'data-tag-name' => t, remote: true %> </span> <% end %> </span> <% end %> 但是如何使用所有新行的正确语法并在ruby和html之间来回进行呢?

1 个答案:

答案 0 :(得分:0)

您可以尝试:

$('#tags').text('Tags:\n' +
                '<% @item.all_tags_list.each do |t| %>\n' +
                '\t<span class="tag-container">\n' +
                '\t\t<%= link_to t, tag_path(t) %>\n' +
                '\t\t<% if @item.tags_from(current_user).include?(t) %>\n' +
                '\t\t\t<span>\n' +
                '\t\t\t\t<%= link_to "×", \'\', class: "remove-tag-button", :\'data-tag-name\' => t, remote: true %>\n' +
                '\t\t\t</span>\n'+
                '\t\t<% end %>\n' +
                '\t</span>\n' +
                '<% end %>\'');
<script src="//code.jquery.com/jquery-1.11.3.js"></script>

<div id="tags" style="white-space: pre;">

</div>