在没有jQuery的选择中嵌入HTML

时间:2017-11-09 01:58:45

标签: javascript

这一定是一个相当直截了当的问题,但我只需要一种简洁的方法,在没有jQuery的情况下围绕给定的文本选择(思考:文本编辑器)嵌入HTML。

下面的代码在嵌入HTML时可以正常工作,但它确实是一个字符串,这显然是一个问题。

replace方法的另一个问题是它只会替换第一个实例,如果用户选择了单个字符或字母和单词的常见组合,则会导致问题。

    const userInput = document.getElementById('user-input'),
          embolden = document.getElementById('controls-embolden')
    let   sel

    userInput.onmouseup = function(){
      if (window.getSelection) {
        sel = window.getSelection().toString()
        embolden.onclick = function(){
          const txt = userInput.innerHTML
          const newTxt = txt.replace(sel, '[b]'+sel+'[/b]')
          userInput.innerHTML = newTxt
        }
      }
    }

我尝试使用createRange,但这不起作用,部分原因是我如何布置我的HTML。

    <body>
      <div id="controls">
        <div class="controls" id="controls-embolden">Embolden</div>
      </div>
      <div id="user-input" contenteditable="true">
        # random text from an article on medium
        By portraying me in a sexual way to attendees, this would have opened the door to additional harassment, and added yet another hurdle I’d have to overcome in order to be perceived as a competent professional. The fact that nowhere along the way did ReactiveConf organizers recognize how this gift could actually be harmful, to me, demonstrated a complete lack of empathy for women in tech. That’s not an organization I want to associate myself or my employer, Meteor Development Group (MDG), with. Not only is the photo itself problematic, but also the fact that ReactiveConf never asked for my consent. I was never informed that ReactiveConf was planning on altering my photo for the event, nor did I see the superhero picture until after day one of the conference had ended. None of the organizers explicitly asked for my permission to display the picture on the big screen. Had I presented my talk on day 1, I would have been completely blindsided as I walked on stage, which is what happened to a colleague of mine who was also unhappy with his picture. Speaking at a conference is already a monumental investment of mental, emotional, and physical effort, right up until the plane ride home. The will to continue investing any more energy into an event whose organizers had showed so little consideration for their speakers vanished as soon as I received the gift. After consulting with trusted members of my team, I decided to withdraw and leave the situation immediately. By portraying me in a sexual way to attendees, this would have opened the door to additional harassment, and added yet another hurdle I’d have to overcome in order to be perceived as a competent professional. The fact that nowhere along the way did ReactiveConf organizers recognize how this gift could actually be harmful, to me, demonstrated a complete lack of empathy for women in tech. That’s not an organization I want to associate myself or my employer, Meteor Development Group (MDG), with.
      </div>
    </body>

我被困在想法中。如何实现这一目标?有什么想法吗?

1 个答案:

答案 0 :(得分:1)

安东尼,我猜你需要使用尖括号而不是方括号,你试图应用粗体标记。

,而不是

const newTxt = txt.replace(sel, '[b]'+sel+'[/b]')

使用

const newTxt = txt.replace(sel, '<b>'+sel+'</b>')