Javascript 如何正确使用jQuery text()函数?

Javascript 如何正确使用jQuery text()函数?,javascript,jquery,text,input,Javascript,Jquery,Text,Input,我想做一个文本区域和表情列表,把表情放到这个文本区域。 我正试图用jquery实现它。 我有这样一个ul元素: <textarea id="TxtEmoji" class="textemoji" placeholder="Type a message here"></textarea> button type="button" id="BtnEmoji" class="btnemoji">&#x1F600;</button> <ul cla

我想做一个文本区域和表情列表,把表情放到这个文本区域。 我正试图用jquery实现它。 我有这样一个ul元素:

<textarea id="TxtEmoji" class="textemoji" placeholder="Type a message here"></textarea>
button type="button" id="BtnEmoji" class="btnemoji">&#x1F600;</button>
<ul class="emoji-list">
                <li><img class="emoji" draggable="false" alt="The image is an image element, not a text node. 

You could use
.html()
to get HTML source code for the image and put that in the text area.

You could use
.find('img').attr('alt')
to get the text alternative to the image (although you seem to have a non-printing character for yours).

You can't display an image in a textarea.

you can use
html()
or
innerHTML
or
textContent()
.you will get the html data to display in text area. if you want to display image use
div
instead.get attribute scr like
$('img').attr('src');
and construct
img
tag with this to show in div.

The
text()
function looks at text inside the tag. Your tag doesn't have text, it has an image with an
alt
attribute. Use this to get the attribute.

$(".emoji-list li img").click(function () {  
    var mm = $(this).attr("alt");  
    var textVal = $("#TxtEmoji").val(); 
    $("#TxtEmoji").val(textVal + mm);     
});  

按钮类型=“按钮”id=“BtnEmoji”类=“BtnEmoji”>😀;

  • 图像是图像元素,而不是文本节点

    您可以使用
    .html()
    获取图像的html源代码并将其放入文本区域

    您可以使用
    .find('img').attr('alt')
    来获取图像的文本替代项(尽管您似乎有一个非打印字符)

    您不能在文本区域中显示图像。

    您可以使用
    html()
    innerHTML
    textContent()
    。您将获得要在文本区域中显示的html数据。如果要显示图像,请改用
    div
    。获取属性scr,如
    $('img').attr('src')并构造
    img
    标记,将其显示在div中。

    函数查看标记内的文本。您的标记没有文本,它有一个带有
    alt
    属性的图像。使用此选项获取属性

    但是,在您的示例中,字符是不可打印的,因此在大多数浏览器中它将显示为方形框