如何更新 jQuery 函数以替换源而不返回?
给定以下插件: https://github.com/Marak/jquery.emoticon.js
作者希望您如何使用以下内容?
$.fn.emoticon = function(theText) {
var imagePath = "emotes/";
var newText = theText;
for( var a in emoticons.emoticon ) {
emoticon = emoticons.emoticon[a];
for( var emote in emoticon.emotes ) {
emote = RegExp.escape(emote);
newText = newText.replace( new RegExp( emote, 'gi' ), '<img src="'+imagePath + emoticon.image + '" />');
}
}
return newText;
};
如果我运行:
$('body').emoticon('Hello :0 World')
它返回:
"Hello <img src="emotes/shock.png" /> World"
有没有办法真正让它替换传递的元素?就像更新时在哪里找到匹配项一样?有点像你将醉酒应用于你想要的元素的程度: $('#example-1').tipsy();
有想法吗?谢谢
Given the following plugin: https://github.com/Marak/jquery.emoticon.js
How does the author expect you to use the below?
$.fn.emoticon = function(theText) {
var imagePath = "emotes/";
var newText = theText;
for( var a in emoticons.emoticon ) {
emoticon = emoticons.emoticon[a];
for( var emote in emoticon.emotes ) {
emote = RegExp.escape(emote);
newText = newText.replace( new RegExp( emote, 'gi' ), '<img src="'+imagePath + emoticon.image + '" />');
}
}
return newText;
};
If I run:
$('body').emoticon('Hello :0 World')
It returns:
"Hello <img src="emotes/shock.png" /> World"
Is there a way to actually have it replace the element passed? As in updating where it finds a match? Kind of like how tipsy you apply tipsy to the element you want: $('#example-1').tipsy();
Thoughts? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 roberkules 所说,这是一个糟糕的 jquery 插件,下面的这个函数将替换 jquery 元素的 html,然后返回它(jquery 元素)以允许链接。
因此,对于 html:
运行
将导致函数返回 $('body') 并且 html 为:
This is a bad jquery plugin as roberkules said, this function below will replace the html of the jquery element and then returns this (the jquery elements) to allow chaining.
So with html:
Running
Would result in the function returning $('body') and the html being: