-6ren">
gpt4 book ai didi

javascript - 插入笑脸文字并播放图像

转载 作者:行者123 更新时间:2023-12-03 07:04:28 24 4
gpt4 key购买 nike

拜托各位,我有一个问题。我创建了一个页面,当单击图像时,可以在文本区域中插入表情文本,当单击提交按钮时,通过 ajax 方法提交表单,以下是运行良好的代码:

    <form id="my_form">
<textarea name="comment" id="comment" class="open" placeholder="Comment..."></textarea><br>
<input type="hidden" id="post" name="post" value="<?php echo $my_id; ?>" />
<span id="form_span"><input type="submit" name="submit" id="submit" class="comment_send" value="COMMENT" /></span><div style="display:none;" id="form_progress"><img src='progress-dots.gif' /></div>
<div id="form_error" style="display:none;"><a href="javascript:;" style="float:right;margin:5px 5px;color:#fff;text-decoration:none;" onclick="document.getElementById('form_error').style.display='none'">x</a>Please make a Comment!</div>
<div id="emoji" class="emoji">
<a href="javascript:;" title=":glad;"><img title="glad" border="0" src="emojis/smiley2.png" /></a>
<a href="javascript:;" title=":joy;"><img title="joy" border="0" src="emojis/smiley3.png" /></a>
<a href="javascript:;" title=":jeer;"><img title="jeer" border="0" src="emojis/smiley43.png" /></a>
<a href="javascript:;" title=":angry;"><img title="angry" border="0" src="emojis/smiley76.png" /></a>
</div>
</form>
<script src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#my_form').submit(function(e) {
e.preventDefault();
if($("#comment").val() == "")
{
$('#comment').focus();
document.getElementById('form_error').style.display='block';
} else {
$.ajax({
method: "GET",
url: "comment_send.php",
data: $(this).serialize(),
beforeSend: function(){
document.getElementById('form_error').style.display='none';
document.getElementById('form_progress').style.display='block';
$('#form_span').html("<input type='submit' name='submit' id='submit' class='disable_comment' value='COMMENT' disabled/>");
},
success: function(status) {
$('#comment').val('');
document.getElementById('form_progress').style.display='none';
$('#form_span').html("<input type='submit' name='submit' id='submit' class='comment_send' value='COMMENT' />");
}
});
}
});
});
</script>
//to insert the emoji texts in the text area
<script type="text/javascript">
$('#emoji a').click(function () {
var smiley = $(this).attr('title');
ins2pos(smiley, 'comment');
});

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();
}
}
</script>

并且我还使用jquery .load函数在同一页面上显示评论:

    <div id="comment_display" style="width:250px;height:500px;overflow-y:auto;"></div>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function () {
$('#comment_display').load('comment_display.php')
}, 1000);
});
</script>

这是comment_display.php页面:

<?php
include 'connect.php';
$query = mysqli_query($con, "SELECT * FROM comments ORDER BY id DESC");
while($row = mysqli_fetch_array($query)){
$comment = $row['comment'];

echo "<p>".$comment."</p>";
}
?>

所有这些代码都工作得很好,但现在我的问题是我想用自己的图像替换显示的表情符号文本,所以我使用这个代码:

        <script type="text/javascript">

// for :glad;
$("body").html($("body").html().replace(/:glad;/g,'<img src="emojis/smiley2.png" />'));

// for :joy;
$("body").html($("body").html().replace(/:joy;/g,'<img src="emojis/smiley3.png" />'));

// for :jeer;
$("body").html($("body").html().replace(/:jeer;/g,'<img src="emojis/smiley43.png" />'));

// for :angry;
$("body").html($("body").html().replace(/:angry;/g,'<img src="emojis/smiley76.png" />'));
</script>

但是,当我添加最后一个 javascript 代码来替换表情符号文本时,将表情符号文本插入文本区域的表情符号图像将停止工作,如果我单击它们,它不会执行任何操作

最佳答案

我创建了一个小演示,仅替换一个单词

$("button").on('click', function() {
var txt = $("textarea").val();
var newtxt=txt.replace(/:glad/g, '<img src="emojis/smiley76.png" />');
$("textarea")[0].innerText=newtxt; //YOU HAVE TO SET NEW TXT IN TEXTAREA
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea cols="5" rows="5">:glad</textarea>
<button>
click
</button>

您可能使用了 $(body).html() 这就是它不起作用的原因。这可能不是您的答案,但您可以了解代码中缺少什么。

关于javascript - 插入笑脸文字并播放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36887823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com