从HTML类中访问Ruby变量并在JS函数中使用它

时间:2017-02-06 22:57:07

标签: javascript html ruby variables tooltip

我试图访问ruby" variablename"我的HTML中的变量:

<% array.each_with_index do |variablename, i| %>
<span class="classname">
    <a ng-href="/variablename/<%= variablename.gsub(' ', '').strip %>"> <%=variablename.strip %> </a>
</span>

在JS工具提示构造函数中:

$('.classname').ready(function(element) {
    Tooltipconstructor.create('.classname', {
      ajax: {
        type: "GET",
        dataType: "JSON",
        url: /api/etc/{} // Where I want to access the HTML/Ruby Variable
     etc..........

如何访问此变量?谢谢!

1 个答案:

答案 0 :(得分:0)

您还可以从a文字中获取值:

$('.classname').ready(function(element) {
  var path = $('.classname > a').text()
  Tooltipconstructor.create('.classname', {
    ajax: {
      type: "GET",
      dataType: "JSON",
      url: /api/etc/+path // Where I want to access the HTML/Ruby Variable
   etc..........