javascript - 如何在我的网站上显示表情符号?

标签 javascript css html

当用户点击下拉菜单时,我想显示表情符号。但问题是我知道如何做到这一点的唯一方法是一个一个地输入表情符号。

我的问题是如何显示所有表情符号?我想当用户单击下拉菜单并单击表情符号时,将其添加到文本区域。

home.php:

<textarea id="textf2" rows="3" maxlength="3000" placeholder="Enter text" cols="35">
</textarea>
<button id="bt6" type="submit" name="search">Post status</button>

<div class="dropdown" id="div1">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">

  </div>
</div>

<script>
  /* When the user clicks on the button, 
  toggle between hiding and showing the dropdown content */
  function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
  }

  // Close the dropdown menu if the user clicks outside of it
  window.onclick = function(event) {
    if (!event.target.matches('.dropbtn')) {
      var dropdowns = document.getElementsByClassName("dropdown-content");
      var i;
      for (i = 0; i < dropdowns.length; i++) {
        var openDropdown = dropdowns[i];
        if (openDropdown.classList.contains('show')) {
          openDropdown.classList.remove('show');
        }
      }
    }
  }
</script>

最佳答案

好的,我想这就是你想要的:

看了之后here并修改了一个小时的代码,我得到了这个:

emojis = document.getElementById("myDropdown").getElementsByTagName("li")

for (var i = 0; i < emojis.length; i++) {
  emojis[i].addEventListener("click", function() {
    var smiley = this.innerHTML;
    ins2pos(smiley, 'textf2');
  });
}

function ins2pos(str, id) {
  var TextArea = document.getElementById(id);
  var val = TextArea.value;
  var before = val.substring(0, TextArea.selectionStart);
  var after = val.substring(TextArea.selectionEnd, val.length);

  TextArea.value = before + str + after;
  setCursor(TextArea, before.length + str.length);

}

function setCursor(elem, pos) {
  if (elem.setSelectionRange) {
    elem.focus();
    elem.setSelectionRange(pos, pos);
  } else if (elem.createTextRange) {
    var range = elem.createTextRange();
    range.collapse(true);
    range.moveEnd('character', pos);
    range.moveStart('character', pos);
    range.select();
  }
}

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn') && !event.target.matches('#myDropdown li')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    for (var i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
#myDropdown {
  display: none
}

#myDropdown.show {
  display: block;
}
<textarea id="textf2" rows="3" maxlength="3000" placeholder="Enter text" cols="35">
</textarea>
<button id="bt6" type="submit" name="search">Post status</button>

<div class="dropdown" id="div1">
    <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <ul id="myDropdown" class="dropdown-content">
    <li>&#x1F601</li>
    <li>&#x1F603</li>
    <li>&#x1F605</li>
  </ul>
</div>

JSfiddle和更多表情符号 here

希望对您有所帮助!

更新:要包含图像,您必须使用 <div id="textf2" contentEditable="true"></div>而不是文本区域并使用标记为 here 的代码

emojis = document.getElementById("myDropdown").getElementsByTagName("li")

for (var i = 0; i < emojis.length; i++) {
  emojis[i].addEventListener("click", function() {
    var smiley = this.innerHTML;
    pasteHtmlAtCaret(smiley + " ");
  });
}

function pasteHtmlAtCaret(html) {
    var sel, range;
    if (window.getSelection) {
        // IE9 and non-IE
        sel = window.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0);
            range.deleteContents();

            // Range.createContextualFragment() would be useful here but is
            // only relatively recently standardized and is not supported in
            // some browsers (IE9, for one)
            var el = document.createElement("div");
            el.innerHTML = html;
            var frag = document.createDocumentFragment(), node, lastNode;
            while ( (node = el.firstChild) ) {
                lastNode = frag.appendChild(node);
            }
            range.insertNode(frag);

            // Preserve the selection
            if (lastNode) {
                range = range.cloneRange();
                range.setStartAfter(lastNode);
                range.collapse(true);
                sel.removeAllRanges();
                sel.addRange(range);
            }
        }
    } else if (document.selection && document.selection.type != "Control") {
        // IE < 9
        document.selection.createRange().pasteHTML(html);
    }
}

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn') && !event.target.matches('#myDropdown img')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    for (var i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
#myDropdown {
  display: none
}
#myDropdown.show {
  display: block;
}
#text_wrapper {
  margin: 40px;
  border: 1px solid #ccc;
}
#textf2 {
  outline: none;
  margin: 10px;
  min-height: 100px;
}

img {
  width: 50px;
  height: auto;
}
<div id="text_wrapper">
  <div id="textf2" contentEditable="true">
  </div>
</div>
<button id="bt6" type="submit" name="search">Post status</button>

<div class="dropdown" id="div1">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <ul id="myDropdown" class="dropdown-content">
    <li><img src="https://upload.wikimedia.org/wikipedia/commons/e/ec/Happy_smiley_face.png" /></li>
    <li><img src="http://res.freestockphotos.biz/pictures/15/15550-illustration-of-a-yellow-smiley-face-pv.png" /></li>
    <li><img src="http://res.freestockphotos.biz/pictures/15/15564-illustration-of-a-yellow-smiley-face-pv.png" /></li>
  </ul>
</div>

关于javascript - 如何在我的网站上显示表情符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41496429/

相关文章:

javascript - 单击链接时可折叠 <div>

javascript - react.js 动态抓取对象导致未定义

javascript - 如何通过代码触发日和周按钮 View 按下?

javascript - 如何使用 bootstrap header 检查内容覆盖

css - 使用 CSS 使 <h1> 垂直居中?

javascript - 无法使用 Javascript 更改 box-ordinal-group CSS 属性

html - 有没有办法在 Bootstrap 中的图片上显示 html 文本?

javascript - jQuery 循环遍历 JSON 对象

javascript - 在 JavaScript 中循环遍历 json 数据

html - 正确对齐列中的内容