LoginSignup
2
4

More than 5 years have passed since last update.

Riot.jsでオプションのテキストにhtmlタグをつけて表示する方法

Posted at

Riot.jsで以下のようにオプションのテキストにhtmlのタグが付いている場合、

tag
<app>

  <!-- layout -->
  <p>{ opts.message }</p>

  <!-- style -->
  <style>
    body {
      color: #444;
      font-size: 16px;
    }
    div {
      margin: 20px;
    }
    #emphasis { 
      color: blue;
      font-weight: bold;
    }
  </style>

</app>
mount
riot.mount('app', {
    message: 'メッセージの<span id="emphasis">この部分</span>が強調'
})

// マウントすると
// メッセージの<span id="emphasis">この部分</span>が強調
// と表示される

そのhtmlタグは評価されずそのまま文言としてそのまま表示されてしまいます。

でも、htmlタグはそれとして評価されてほしい。(上記では、spanタグ)
どうするか考えた結果、innerHTMLでガツンとセットする方法が一番楽。

tag(改良版)
<app>

  <!-- layout -->
  <p id="message"></p>

  <!-- logic -->
  <script>

    // riotはid属性にthisでアクセスできる
    this.message.innerHTML = opts.message

  </script>

  <!-- style -->
  <!-- 先ほどと同じなので省略 -->

</app>

以上、ちょっとした小技でした。 →デモ

2
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4