简体   繁体   English

jQuery noconflict保护电子邮件垃圾邮件

[英]jquery noconflict for email spam protection

I'm running into some conflicts as far as running the jquery noconflict setting on multiple functions, so I decided to isolate the problem by testing the noconflict against one of the functions I'm testing out. 就在多个功能上运行jquery noconflict设置而言,我遇到了一些冲突,因此我决定通过针对要测试的功能之一测试noconflict来隔离问题。

I've tried multiple variations on where I was placing the s$ however none of the configurations seem to work. 我在放置s $的位置上尝试了多种变体,但是这些配置似乎都不起作用。 The only way I can get this work is by leaving the variable as -> var $, and all of the dependent variables to this setting, however I need to find out how I can get this to work using a unique variable? 我可以完成这项工作的唯一方法是将变量保留为-> var $,并将所有因变量保留为此设置,但是我需要找出如何使用唯一变量使它起作用的方法?

Perhaps there's an issue with my syntax as well? 也许我的语法也有问题?

var s$ = jQuery.noConflict();

s$.fn.emailSpamProtection = function(className) {

return s$(this).find("." + className).each(function() {
var $email = s$(this);
var address = $email.text()
.replace(/\s*\[at\]\s*/, '@')
.replace(/\s*\[dot\]\s*/g, '.');
$email.html('<a href="mailto:' + address + '">'+ address +'</a>');
    });
};

Here's the revised script I tried out. 这是我尝试过的修订脚本。

jQuery.noConflict();

(function($){
$.fn.emailSpamProtection = function(className) {
 return this.find("." + className).each(function() {
        var $email = this;
        var address = $email.text()
        .replace(/\s*\[at\]\s*/, '@')
        .replace(/\s*\[dot\]\s*/g, '.');
        $email.html('<a href="mailto:' + address + '">'+ address +'</a>');
    });
};
})(jQuery);

And I placed this into my .html homepage 而我把这个变成我的.html主页

jQuery(function($){

    //Note, you can use $(...) because you are wrapping everything within a jQuery function
    $("body").emailSpamProtection("email");

});

I don't think you're using the noConflict attribute correctly. 我认为您没有正确使用noConflict属性。 This is how I would use it: 这就是我将如何使用它:

//Establish jQuery noConflict mode.
jQuery.noConflict();

//Define your jQuery plugins/functions
(function($){
$.fn.emailSpamProtection = function(className) {
 return this.find("." + className).each(function() {
        var $email = this;
        var address = $email.text()
        .replace(/\s*\[at\]\s*/, '@')
        .replace(/\s*\[dot\]\s*/g, '.');
        $email.html('<a href="mailto:' + address + '">'+ address +'</a>');
    });
};
})(jQuery);


// Use jQuery with $(...)
jQuery(function($){

    //Note, you can use $(...) because you are wrapping everything within a jQuery function
    $('#myElement').emailSpamProtection();

});

Figured it out. 弄清楚了。 After doing some trial and error, I was able to resolve the issue by flipping the variable to $j as opposed to j$ . 经过反复试验后,我能够通过将变量翻转为$j而不是j$来解决此问题。 Here's my final result. 这是我的最终结果。

//JQuery Section

var $j=jQuery.noConflict();

  //Hiding other scripts that were included in this application.js file//


//email spam protection - Example Markup: <span class="email">name[at]domain[dot]com</span>
$j.fn.emailSpamProtection = function(className) {

return $j(this).find("." + className).each(function() {
var email = $j(this);
var address = email.text()
.replace(/\s*\[at\]\s*/, '@')
.replace(/\s*\[dot\]\s*/g, '.');
email.html('<a href="mailto:' + address + '">'+ address +'</a>');
    });
};

});

//Script added to the presentation page (html,php,whatever)

<script>
$j(function() {

  $j("body").emailSpamProtection("email"); 

});
</script>

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

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