【问题标题】:How to open Action Text Trix attachments or links in new window (target _blank)如何在新窗口中打开 Action Text Trix 附件或链接(目标 _blank)
【发布时间】:2021-05-05 05:14:16
【问题描述】:

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

class Post < ApplicationRecord
  has_rich_text :rich_text_content
end

【问题讨论】:

    标签: ruby-on-rails trix actiontext stimulusjs


    【解决方案1】:

    添加一个刺激控制器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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多