简体   繁体   English

如何使用jQuery更改特定内容?

[英]How to change a specific content with jQuery?

I want to add emotions to my blog. 我想在我的博客中添加情感。 When I write ":)" on a post, it should change into an image automatically. 当我在帖子上写“ :)”时,它应该自动更改为图像。 But I don't know how to do it. 但是我不知道该怎么做。

The real problem is, I don't know how to identify all ":)" emotions. 真正的问题是,我不知道如何识别所有“ :)”情绪。 I can do the rest, if I do that. 如果可以的话,我可以做的剩下的。 So, anyone can help me? 那么,有人可以帮助我吗?

Try checking out :contains() selector. 尝试签出:contains()选择器。

$(".article:containts(':)')")

After that, you can store the text in a variable, manipulate it (find & replace), replace the text with an image, and overwrite the element's html with the new manipulated html. 之后,您可以将文本存储在变量中,进行处理(查找和替换),用图像替换文本,然后用新的经处理的html覆盖元素的html。

https://api.jquery.com/contains-selector/ https://api.jquery.com/contains-selector/

Try google: https://os.alfajango.com/css-emoticons/ 尝试使用Google: https//os.alfajango.com/css-emoticons/

Never used it but looks good. 从未使用过,但看起来不错。

I have googled your query and found some helpful libraries. 我用谷歌搜索了您的查询,发现了一些有用的库。 Please find link below: 请在下面找到链接:

http://hassankhan.me/emojify.js/ http://hassankhan.me/emojify.js/

https://www.npmjs.com/package/emoticons-js https://www.npmjs.com/package/emoticons-js

您可以使用jquery选择器选择所有包含':)'的元素,然后尝试将其剪切并替换为一些情感​​图标。

$('p:contains(\':)\')')

Assuming this code: 假设此代码:

<div id="post_1" class="post">
   Nice to meet you :) Bye bye!
</div>

You can replace the emoticon using .indexOf() and .replace() combined. 您可以结合使用.indexOf().replace()来替换图释。

var contentReplaced = $("#post_1").html().toString().replace(":)", "<img src='path_to_image' />");
$("#post_1").html(contentReplaced);

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

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