简体   繁体   English

处理'img'click事件,并使用jQuery将'alt'属性插入文本框

[英]Handle 'img' click event and insert 'alt' attribute into textbox using jQuery

I'm creating a web chat page using PHP and jQuery, and I am adding an emoticon feature, which everything is done except for the jQuery portion. 我正在使用PHP和jQuery创建一个Web聊天页面,并添加了表情功能,除jQuery部分外,其他所有操作均已完成。 My problem is I'm not getting a click event when I click on the emoticon image. 我的问题是,当我单击图释图像时,没有点击事件。 In the handler it is supposed to get the current message value, get the text version of the emoticon from the 'alt' attribute, and set the textbox value to its previous value + the emoticons text appended. 在处理程序中,应该获取当前消息值,从'alt'属性获取表情符号的文本版本,并将文本框值设置为其先前值+附加的表情符号文本。

Here is what I tried: 这是我尝试过的:

(Each button appears as so: <img class="embtn" src="emoticons/happy.gif" alt=":)" /> ) (每个按钮显示为: <img class="embtn" src="emoticons/happy.gif" alt=":)" />

$(document).ready(function(){
    // Insert Emoticon
    $(".embtn").click(function(event){
        var prevMsg = $("#usermsg").val();
        var emotiText = event.target.attr("alt");
        $("#usermsg").val(prevMsg + emotiText);
    });
});

How should I go about doing this? 我应该怎么做呢?

The version of jQuery I am using is 1.3. 我正在使用的jQuery版本是1.3。

Try changing: 尝试更改:

var emotiText = event.target.attr("alt");

to: 至:

var emotiText = $(event.target).attr("alt");

In the future check the console for errors when you are stumped. 将来当您陷入困境时,请检查控制台是否有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM