(Rails) 如何替换“link_to_remote”的文本 不破坏链接?

发布于 2024-07-29 10:56:32 字数 730 浏览 9 评论 0原文

是否有一种高质量的方法来更新“link_to_remote”内的文本并保持链接功能? 基本上我有两个链接:

  <%= link_to_remote "(#{building.charts.size} Charts)",{:url => {:action => "update_chart_matrix", :chartable_type => "building",:chartable_id => building.id, :title => building.name},
    :update => 'chart-matrix',
}
%>

...和...

<%= link_to_remote "Add Chart",{:url => {:action => "add_chart_for_chartable", :chartable_type => "building",:chartable_id => building.id},
    :update => 'other_link', #really not sure about this part as I only want to update the Chart Count in the other link
}
%>

只需替换链接内的 HTML 就足够容易了,但我不想“破坏”其功能。 有任何想法吗?

谢谢。

Is there a quality way to update the text inside a "link_to_remote" and leave the link functional? Basically I have two links:

  <%= link_to_remote "(#{building.charts.size} Charts)",{:url => {:action => "update_chart_matrix", :chartable_type => "building",:chartable_id => building.id, :title => building.name},
    :update => 'chart-matrix',
}
%>

...and...

<%= link_to_remote "Add Chart",{:url => {:action => "add_chart_for_chartable", :chartable_type => "building",:chartable_id => building.id},
    :update => 'other_link', #really not sure about this part as I only want to update the Chart Count in the other link
}
%>

It would be easy enough to simply replace the HTML inside the link, but I don't want to "break" its functionality. Any ideas?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2024-08-05 10:56:32

更新链接的内部 HTML 不会破坏 onclick 功能。 您正在原型中使用更新(通过Rails),它设置了innerHTML:

update: function(element, content) {
  element = $(element);
  if (content && content.toElement) content = content.toElement();
  if (Object.isElement(content)) return element.update().insert(content);
  content = Object.toHTML(content);
  // This sets innerHTML, it doesn't destroy the object
  element.innerHTML = content.stripScripts();
  content.evalScripts.bind(content).defer();
  return element;
},

只要返回的内容适合存在于a标签内,您应该没问题。

祝你好运!

Updating the internal HTML of a link won't break the onclick functionality. You're using update in prototype (via Rails), which set's the innerHTML:

update: function(element, content) {
  element = $(element);
  if (content && content.toElement) content = content.toElement();
  if (Object.isElement(content)) return element.update().insert(content);
  content = Object.toHTML(content);
  // This sets innerHTML, it doesn't destroy the object
  element.innerHTML = content.stripScripts();
  content.evalScripts.bind(content).defer();
  return element;
},

As long as the content coming back is suitable to live inside an a tag, you should be fine.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文