2

如何使我在 Trix(ActionText)编辑器中添加的链接在 target="_blank" 的新窗口中打开链接?

class Post < ApplicationRecord
  has_rich_text :rich_text_content
end
4

1 回答 1

3

添加刺激控制器richtext_controller.js

import { Controller } from "stimulus"

export default class extends Controller {
  connect() {
    this.element.querySelectorAll('a').forEach(function(link) {
      if (link.host !== window.location.host) {
        link.target = "_blank"
      }
    })
  }
}

像这样添加您的内容:

<div data-controller="richtext">
  <%= post.rich_text_content %>
</div>
于 2021-02-01T00:36:37.727 回答